繁体   English   中英

Jquery .validate require_from_group

[英]Jquery .validate require_from_group

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

还有一种方法可以将“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规则,则所有其他字段验证都可以正常工作。

感谢帮助。

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

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

事实证明,这也解决了与的1.10.0版本发布的登录GitHub的错误require_from_groupadditional-method-1.10.jsjquery.validation

github问题: require_from_group禁用其他规则

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

这篇帖子可以节省时间,因为这会花费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."));

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM