.valid() 仅使用 jQuery 验证插件验证第一个输入

发布于 2024-09-13 08:43:37 字数 1459 浏览 2 评论 0原文

我很难让 jQuery 验证插件按我想要的方式工作。

我拥有的是一个包含多个必需输入的表单,规则如下所示:

$("#makeEvent").validate({
    onfocusout: false,
    rules: {
        name: {
            required: true,
            minLength: 2,
            maxLength: 100
        },
        host: {
            required: true,
            minLength: 2,
            maxLength: 100
        },
        startDate: {
            required: true,
            date: true
        },
        endDate: {
            required: true,
            date: true
        },
        desc: {
            required: true,
            minLength: 10,
            maxLength: 255
        },
        webpage: {
            required: false,
            url: true
        },
        email: {
            required: true,
            email: true
        },
    }
});

现在我有一个自定义 .click() 调用,我想在其中验证表单,然后在允许用户发送表单之前向用户显示预览。请看下面的函数:

$('#submitPreview').click(function() {
    if($("#makeEvent").valid() === false) {
        //What to do if validation fails
    } else if($("#makeEvent").valid() === true) {
        //Show the preview and let the user either make changes or submit the final result
    }
});

但是发生的情况是,该函数只验证第一个输入(名称),甚至验证错误(规则要求至少 2 个字符,但它仍然只验证输入的 1 个字符。只有在以下情况下才返回 false:根本没有添加任何字符)。

也许我的方法不是最好的,所以我愿意接受任何建议。如果您想查看混乱的源代码,您可以在此处查看正在运行的函数: http:// /event.gusthlm.se/make.php

I'm having a hard time getting the jQuery Validation plugin to work as I want.

What I have is a form with several required inputs, rules as showed below:

$("#makeEvent").validate({
    onfocusout: false,
    rules: {
        name: {
            required: true,
            minLength: 2,
            maxLength: 100
        },
        host: {
            required: true,
            minLength: 2,
            maxLength: 100
        },
        startDate: {
            required: true,
            date: true
        },
        endDate: {
            required: true,
            date: true
        },
        desc: {
            required: true,
            minLength: 10,
            maxLength: 255
        },
        webpage: {
            required: false,
            url: true
        },
        email: {
            required: true,
            email: true
        },
    }
});

Now i have a custom .click() call where I want to validate the form and then show a preview for the user before allowing them to send the form. Please see the function below:

$('#submitPreview').click(function() {
    if($("#makeEvent").valid() === false) {
        //What to do if validation fails
    } else if($("#makeEvent").valid() === true) {
        //Show the preview and let the user either make changes or submit the final result
    }
});

But what happens is that the function only validates the first input (name) and even validates it incorrectly (the rules requires a minimum of 2 characters, still it validates with only 1 character entered. It only return false if no character is added at all).

Maybe my approach isn't the best, so I'm open to any suggestions at all. If you want to have a look at the messy source code you can see the function in action here: http://event.gusthlm.se/make.php

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

百善笑为先 2024-09-20 08:43:37

我看到的两个问题是,您没有在输入字段中使用 name 属性,并且使用 name 作为 ID 之一(导致各种悲伤)对于表单提交上的 IE)。

我建议将名称属性添加到您的输入、选择和文本区域元素中,并将“名称”更改为“thename”之类的内容:

<label for="thename">Börjar</label>
<input type="text" name="thename" id="thename"/>

Two problems I can see are that you're not using the name attribute on your input fields and you're using name as one of the IDs (causes all sorts of grief for IE on form submit).

I would suggest adding the name attributes to your input, select and textarea elements and changing "name" to something like "thename":

<label for="thename">Börjar</label>
<input type="text" name="thename" id="thename"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文