Jquery .validate require_from_group [英] Jquery .validate require_from_group
问题描述
每当我使用 require_from_group
时,它都会禁用所有其他验证。有什么想法吗?
Whenever i use require_from_group
it disables all other validations. Any ideas why?
还有一种方法可以将Telefon和Mobitel分组并应用 require_from_group
来它?
Also is there a way to group "Telefon" and "Mobitel" and apply require_from_group
to it?
$(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
规则,则所有其他字段验证都可以正常工作。
All other fields not included here use simple "required" class. If i remove require_from_group
rules applied to "Telefon" and "Mobitel" all other field validations work fine.
谢谢求助。
编辑 html: http ://cl.ly/29391q0Q3G231T2I380m (这里发布的时间太长)
EDIT html : http://cl.ly/29391q0Q3G231T2I380m (too long to post it here)
推荐答案
@Tats_innit发布了一个自定义 require_from_group
此处: https://stackoverflow.com/posts/13127475
@Tats_innit posted a custom require_from_group
here: https://stackoverflow.com/posts/13127475
事实证明,这也修复了在 require_from_group
版本1.10.0中发布的已记录的github错误 additional-method-1.10.js
for jquery.validation
。
Turns out, this also fixes a logged github bug that was released with version 1.10.0 of require_from_group
in additional-method-1.10.js
for jquery.validation
.
github问题:
require_from_group禁用其他规则
github issue: require_from_group disables other rules
smileyanp @ github引用了th在他的解决方案中发布了他重用@Tats_innit的功能并创建了一个测试,显示它正常工作并且不会对 require_from_group
之前定义的其他规则进行验证。
smileyanp@github quoted this post in his solution where he reused @Tats_innit's function and created a test that showed it works correctly and doesn't disable validation on other rules defined prior to the require_from_group
.
这帖子在这里节省了时间,因为这会花费3小时的谷歌搜索这么小的细节..
This post is here as a time saver as this burned 3 hrs of googling for such a small detail..
FIX:
只需更新 additional-method-1.10.js
或在 additional-method-1.10.js之后执行此代码
已加载(以覆盖该函数)。
Just update additional-method-1.10.js
or execute this code after additional-method-1.10.js
has loaded (to overwrite the function).
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."));
这篇关于Jquery .validate require_from_group的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!