JQuery Validation Plugin:valid()为非必填字段返回false

时间:2020-11-29 11:24:58

The valid() function states: "Checks whether the selected form is valid or whether all selected elements are valid."

valid()函数指出:“检查所选表单是否有效或所有选定元素是否有效。”

Which seems to imply we can test for a group of controls using a selector (we're testing controls on a asp.net form) e.g.

这似乎意味着我们可以使用选择器测试一组控件(我们正在测试asp.net表单上的控件),例如

$('div[id$=pnlBillingInfo] .Field input').valid()

This works if all fields are required. However, if one field requires no validation (required = false) then valid() returns false when this field is left blank.

如果需要所有字段,则此方法有效。但是,如果一个字段不需要验证(required = false),则当此字段留空时,valid()将返回false。

This behavior doesn't seem correct - are we missing something? We should be able to leave these optional fields blank and still have the group of controls validate ok.

这种行为似乎不正确 - 我们错过了什么吗?我们应该能够将这些可选字段留空,并且仍然可以使控件组验证正常。

If we try to validate this optional field - the result is 'undefined' e.g.

如果我们尝试验证此可选字段 - 结果为'未定义',例如

$("#aspnetForm").validate().element('input[id$=txtBillingAddress2]')

jQuery 1.4.2

jQuery plugin Validation 1.7 (latest version)

jQuery插件验证1.7(最新版本)

http://bassistance.de/jquery-plugins/jquery-plugin-validation/

Thanks!

2 个解决方案

#1


1  

I've found it useful to actually separate the form validation rules from the validation event (ie: the moment when the form is submitted).

我发现实际将表单验证规则与验证事件分开是有用的(即:表单提交的时刻)。

You need to set up rules for the form - either in the JS or in the HTML via classes on each input / select / etc.

您需要为表单设置规则 - 在JS或HTML中通过每个输入/选择/等的类。

I prefer doing it via JS, because it keeps the DOM clean; also, I usually put the form's rules outside the $(document).ready() function:

我更喜欢通过JS来做,因为它保持了DOM的清洁;另外,我通常将表单的规则放在$(document).ready()函数之外:

$('#form').validate({
    rules: {
        [...]
    },
    messages: {
        [...]
    }
});

This actually does nothing functional. It just sets the rules. When you need to actually validate the form, you can check these rules by firing the .valid() event:

这实际上没有任何功能。它只是设定规则。当您需要实际验证表单时,可以通过触发.valid()事件来检查这些规则:

$(document).ready(function(){
    $('#form').submit(function(){

            // Check validity
            if (!$(this).valid()) return false;

            // Form is valid, so submit it!
            [...]
    });
});

#2


1  

There's a bug on the library when rule methods check for optional fields with "this.optional(element)". When the field is empty this.optional returns "dependency-mismatch" and the validation method returns "undefined" marking the field as invalid.

当规则方法使用“this.optional(element)”检查可选字段时,库中存在错误。当该字段为空时,this.optional返回“dependency-mismatch”,验证方法返回“undefined”,将该字段标记为无效。

Full explanation here:

这里有完整的解释

https://github.com/jzaefferer/jquery-validation/issues/481

#1


1  

I've found it useful to actually separate the form validation rules from the validation event (ie: the moment when the form is submitted).

我发现实际将表单验证规则与验证事件分开是有用的(即:表单提交的时刻)。

You need to set up rules for the form - either in the JS or in the HTML via classes on each input / select / etc.

您需要为表单设置规则 - 在JS或HTML中通过每个输入/选择/等的类。

I prefer doing it via JS, because it keeps the DOM clean; also, I usually put the form's rules outside the $(document).ready() function:

我更喜欢通过JS来做,因为它保持了DOM的清洁;另外,我通常将表单的规则放在$(document).ready()函数之外:

$('#form').validate({
    rules: {
        [...]
    },
    messages: {
        [...]
    }
});

This actually does nothing functional. It just sets the rules. When you need to actually validate the form, you can check these rules by firing the .valid() event:

这实际上没有任何功能。它只是设定规则。当您需要实际验证表单时,可以通过触发.valid()事件来检查这些规则:

$(document).ready(function(){
    $('#form').submit(function(){

            // Check validity
            if (!$(this).valid()) return false;

            // Form is valid, so submit it!
            [...]
    });
});

#2


1  

There's a bug on the library when rule methods check for optional fields with "this.optional(element)". When the field is empty this.optional returns "dependency-mismatch" and the validation method returns "undefined" marking the field as invalid.

当规则方法使用“this.optional(element)”检查可选字段时,库中存在错误。当该字段为空时,this.optional返回“dependency-mismatch”,验证方法返回“undefined”,将该字段标记为无效。

Full explanation here:

这里有完整的解释

https://github.com/jzaefferer/jquery-validation/issues/481