Jquery .validate require_from_group

每当我使用require_from_group它都会禁用所有其他validation。 有什么想法吗?

还有一种方法可以将“Telefon”和“Mobitel”分组并将require_from_group应用于它吗?

  $(document).ready(function(){ $("#fncMain").validate( { /*groups:{Call:"Telefon Mobitel"},*/ rules:{ Davcna:{required:true,exactlength:5, digits:true}, Idzav:{required:true,exactlength:5, digits:true}, Maticna:{required:true,exactlength:5, digits:true}, Telefon:{require_from_group: [1,".callme"]}, Mobitel:{require_from_group: [1,".callme"]} }, messages:{ }} ); }); 

此处未包含的所有其他字段使用简单的“必需”类。 如果我删除了应用于“Telefon”和“Mobitel”的require_from_group规则,则所有其他字段validation都可以正常工作。

感谢帮助。

编辑 HTML: http : //cl.ly/29391q0Q3G231T2I380m (太长了,不能在这里发布)

@Tats_innit在这里发布了一个自定义的require_from_group : https ://stackoverflow.com/posts/13127475

事实certificate,这也修复了在additional-method-1.10.js使用版本1.10.0的require_from_group发布的已记录的github错误。

github问题: require_from_group禁用其他规则

smileyanp @ github在他的解决方案中引用了这篇文章,他重用@Tats_innit的函数并创建了一个测试 ,certificate它正常工作, 并且不会禁用 require_from_group之前定义的其他规则的validation。

这篇post可以节省时间,因为这会花费3小时的谷歌搜索这么小的细节。

固定:

只需更新additional-method-1.10.js或在加载additional-method-1.10.js后执行此代码(覆盖该函数)。

 jQuery.validator.addMethod("require_from_group", function(value, element, options) { var numberRequired = options[0]; var selector = options[1]; var fields = $(selector, element.form); var filled_fields = fields.filter(function() { // it's more clear to compare with empty string return $(this).val() != ""; }); var empty_fields = fields.not(filled_fields); // we will mark only first empty field as invalid if (filled_fields.length < numberRequired && empty_fields[0] == element) { return false; } return true; // {0} below is the 0th item in the options field }, jQuery.format("Please fill out at least {0} of these fields.")); 

编辑 :实际上看起来这个修复已经合并到版本1.12.0中,你可以在这里找到它的CDN指针: http//jqueryvalidation.org/

供参考:

 http://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/jquery.validate.js http://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/jquery.validate.min.js http://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/additional-methods.js http://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/additional-methods.min.js 

在找到上面的解决方案之前,我在下面找到了这个代码,所以我的建议是使用上面引用的CDN链接,而不是将下面的代码粘贴到JS文件中。

现在有一个更好的修复GitHub (滚动到最底部),我在这里复制了。 这不是我的工作 ,编写它的GitHub用户sfreytag,似乎不是SO的贡献者,我只是想把它变成SO所以其他发现这个的人不必在GitHub上挖掘线程:

 jQuery.validator.addMethod("require_from_group", function(value, element, options) { var validator = this; var selector = options[1]; var validOrNot = $(selector, element.form).filter(function() { return validator.elementValue(this); }).length >= options[0]; if(!$(element).data('being_validated')) { var fields = $(selector, element.form); fields.data('being_validated', true); fields.valid(); $(element.form).valid(); fields.data('being_validated', false); } return validOrNot; }, jQuery.format("Please fill at least {0} of these fields.")); 

到目前为止,我已经对此进行了有限的测试,但它似乎正如您所期望的那样工作,所有validation都会发生(而不是像以前一样通过任何非“require_from_group”validation),所以到目前为止我很满意。 我刚刚在JS代码顶部的validation器声明之后添加它:

 $.validator.setDefaults({ debug: true, success: "valid" }); jQuery.validator.addMethod("require_from_group", function(value, element, options) { var validator = this; var selector = options[1]; //continuation of code...