.valid()仅使用jQuery Validation插件来验证第一个输入 [英] .valid() only validates first input with jQuery Validation plugin

查看:190
本文介绍了.valid()仅使用jQuery Validation插件来验证第一个输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难让jQuery Validation插件按我的意愿工作.

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
        },
    }
});

现在,我有一个自定义.click()调用,我想在其中验证表单,然后在允许用户发送表单之前为用户显示预览.请查看以下功能:

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
    }
});

但是发生的是该函数仅验证第一个输入(名称),甚至验证不正确(规则要求至少2个字符,但仍仅输入1个字符进行验证.如果不输入任何字符,则仅返回false完全添加).

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).

也许我的方法不是最好的,所以我完全可以接受任何建议.如果您想看一下凌乱的源代码,可以在这里看到正在使用的函数: http://event.gusthlm.se/make.php

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

推荐答案

我看到的两个问题是您没有在输入字段上使用name属性,而您正在使用name作为其中之一ID(会导致IE在表单提交时遇到种种麻烦).

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).

我建议将名称属性添加到您的输入中,选择和textarea元素,并将名称"更改为"thename":

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"/>

这篇关于.valid()仅使用jQuery Validation插件来验证第一个输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆