简体   繁体   English

仅当使用jquery验证插件选中复选框时,才将minLength设置为字段

[英]setting a minLength to a field only when a checkbox has been checked using the jquery validation plugin

I'm using this jquery validation plugin . 我正在使用这个jquery 验证插件 I would like to set a minLength property of 5 to a field only when a checkbox is selected. 我想仅在选中复选框时才将minLength属性设置为5。 I understand the "depends", but how do I use it along with a value of 5. 我理解“依赖”,但是如何将其与5一起使用。

 "txt-alt-zip": {
            required: {                                                             // the zip is required if the alternate billing address is checked
                depends: function (element) {
                    return $("#chk-alt-billing-address").attr("checked");
                }

            },
            minlength: {                                                             // the zip is required if the alternate billing address is checked
                depends: function (element) {
                    return $("#chk-alt-billing-address").attr("checked");
                }

            }

        }

Here you go 干得好

'txt-alt-zip': {
  minlength: {
    param: 4,
    depends: '#chk-alt-billing-address:checked'
  }
}

Note that You can do it with any type of validation rules. 请注意,您可以使用任何类型的验证规则来执行此操作。

From your request, this makes more sense to me: 根据您的要求,这对我来说更有意义:

 "txt-alt-zip": {
   minlength: function() {
     $result = 0;
     if ($("#chk-alt-billing-address").attr("checked")) $result = 4;
     return $result;
   }
 }

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

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