Jquery 验证依赖字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3975778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:27:41  来源:igfitidea点击:

Jquery validate depend field

jqueryvalidationdepends

提问by cesar

Huy Guys,

嘿伙计们,

I am trying to use jQuery validation plugin and specify that one field is required only when 3 other fields are blank (not filled).

我正在尝试使用 jQuery 验证插件并指定仅当其他 3 个字段为空(未填充)时才需要一个字段。

the thing is that the rule is working but it still remain requiring the field even when I type value on it.

问题是规则正在起作用,但即使我在其上键入值,它仍然需要该字段。

Thanks.

谢谢。

This is my code:

这是我的代码:

$("#ProspectDtlFrm").validate({
 rules: {

  prsptEmail: {
   required: function(element) { return ( $("#prsptHomePhone").val() == '' && $("#prsptBusinessPhone").val() == '' && $("#prsptMobilePhone").val() =='') }                                         
   }    
 },
 messages: {
      prsptEmail: "Please enter your first name"
 }
}); 

回答by Darin Dimitrov

You could use depends:

你可以使用depends

$('#ProspectDtlFrm').validate({ 
    rules: {
        prsptEmail: {
            required: {
                depends: function(element) {
                    return ($('#prsptHomePhone').val() == '' && 
                            $('#prsptBusinessPhone').val() == '' && 
                            $('#prsptMobilePhone').val() == '');
                }
            }
        }    
    }, 
    messages: { 
        prsptEmail: 'Please enter your first name'
    } 
});

回答by vicman4

The dependsclause is still supported. This is from 1.11.0pre version:

depends仍然支持该条款。这是来自 1.11.0pre 版本:

normalizeRules: function(rules, element) {
  // handle dependency check
  $.each(rules, function(prop, val) {
    // ignore rule when param is explicitly false, eg. required:false
    if (val === false) {
      delete rules[prop];
      return;
    }
    if (val.param || val.depends) {
      var keepRule = true;
      switch (typeof val.depends) {
        case "string":
          keepRule = !!$(val.depends, element.form).length;
          break;
        case "function":
          keepRule = val.depends.call(element, element);
          break;
      }
      if (keepRule) {
        rules[prop] = val.param !== undefined ? val.param : true;
      } else {
        delete rules[prop];
      }
    }
  });

As you can see, yo can choose between "string" and "function". So you can use:

如您所见,您可以在“字符串”和“函数”之间进行选择。所以你可以使用:

rules: {
  input_name_here: {
    required: {
      depends: function(element) {
        return $("#thickBoxId:checked").length;
        //or whatever you need to check
      }
    }
  }
}

Or

或者

rules:{
  input_name_here:{
    required: '#tickBoxId:checked'
  }
}

If you only need to test for the existence of something checked.

如果您只需要测试检查的东西是否存在。

回答by joe

'depends' seems like it's not supported any more, and, their docs are not up to date.

'depends' 似乎不再受支持,而且他们的文档不是最新的。

So in the scenario where if a 'yes' tick box is checked, validate another field:

因此,在选中“是”复选框的情况下,验证另一个字段:

rules:{
    'name value of area you want to validate':{
        required: '#tickBoxId:checked'
    }
}

回答by enlacee

the way is dependsit's works.

方法是depends它的作品。

    rules: {
        prsptEmail: {
            required: {
                depends: function(element){
                    if ($('#id-user').val() == '') {
                        return true;
                    } else {
                        return false;
                    }
                }
            }
        },
    },
    messages: {
        prsptEmail : {
            required : "please signIn!",
        }
    },