基于jQuery.validator.addMethod的字段上的条件规则 [英] Conditional rules on the fields based on jQuery.validator.addMethod

查看:109
本文介绍了基于jQuery.validator.addMethod的字段上的条件规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个jQuery.validator.addMethod,分别命名为"A"和"B".

是否可以基于不同的方法在字段上设置条件规则?例如:

$("#form2").validate( {
    rules: {
        inputVal: {
            //if condition = true, A:true, else run B:true
        }
    }
     ...
});

解决方案

是否可以基于不同的方法在字段上设置条件规则?

否,您不能基于.validate()方法中的不同条件设置不同的规则.每个字段的对象文字只能包含用逗号分隔的key:value对列表,代表所需的方法和参数.

但是,您可以使用depends属性为每种方法有条件地设置参数,通过打开或关闭规则来有效地实现相同的目的...

$("#form2").validate( {
    rules: {
        inputVal: { // <- field name
            method_A: 
                depends: function(element) {
                    // activate/disable method A
                    return (condition) ? true : false;
                }
            },
            method_B: 
                depends: function(element) {
                    // disable/activate method B
                    return (condition) ? false : true;
                }
            }
        }
    },
     ...
});

演示: http://jsfiddle.net/nL2sk069/


否则,您可以简单地编写一个结合了方法A&的功能的新方法. B,然后在那里评估您的状况.

var message;

$.validator.addMethod('method_C', function(value, element, param) {
    var result;
    if (condition) {
       // validate value as per A
       result = (value === "A") ? true : false;  // (pass or fail)
       message = "custom message A";
    } else {
       // validate value as per B
       result = (value === "B") ? true : false;  // (pass or fail)
       message = "custom message B";
    }
    return this.optional(element) || result;
}, function() { return message; });

演示: http://jsfiddle.net/3uheeetx/

I have two jQuery.validator.addMethod, named "A" and "B".

Is it possible to set conditional rules on the fields based on different methods? For example:

$("#form2").validate( {
    rules: {
        inputVal: {
            //if condition = true, A:true, else run B:true
        }
    }
     ...
});

解决方案

Is it possible to set conditional rules on the fields based on different methods?

No, you cannot set different rules based on different conditions within the .validate() method. The object literal for each field can only contain a comma separated list of key:value pairs representing the desired methods and parameters.

However, you are allowed to conditionally set the parameter for each method using the depends property, effectively achieving the same thing by toggling the rule on or off...

$("#form2").validate( {
    rules: {
        inputVal: { // <- field name
            method_A: 
                depends: function(element) {
                    // activate/disable method A
                    return (condition) ? true : false;
                }
            },
            method_B: 
                depends: function(element) {
                    // disable/activate method B
                    return (condition) ? false : true;
                }
            }
        }
    },
     ...
});

DEMO: http://jsfiddle.net/nL2sk069/


Otherwise, you could simply write a new method that combines the functionality of methods A & B, and just evaluate your condition there.

var message;

$.validator.addMethod('method_C', function(value, element, param) {
    var result;
    if (condition) {
       // validate value as per A
       result = (value === "A") ? true : false;  // (pass or fail)
       message = "custom message A";
    } else {
       // validate value as per B
       result = (value === "B") ? true : false;  // (pass or fail)
       message = "custom message B";
    }
    return this.optional(element) || result;
}, function() { return message; });

DEMO: http://jsfiddle.net/3uheeetx/

这篇关于基于jQuery.validator.addMethod的字段上的条件规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆