简体   繁体   English

jQuery验证插件1.11.1>规则>自定义逻辑运算符(=> rule1或rule2或rule3…)

[英]jQuery Validation Plugin 1.11.1 > rules > custom logical operators (=> rule1 OR rule2 OR rule3…)

To give an example, how can I define a field that can be blank OR digits OR "foo" ? 举个例子,我如何定义一个可以为空或数字或“ foo”的字段?

I currently have this: 我目前有这个:

var myValidator= jQuery2("#myFormId").validate({
    rules: {
        myField: {
            digits: true,
            required: false,
            maxlength: 9999,
            myCustomRule: true
        }
}});
jQuery2.validator.addMethod("myCustomRule", function(value, element) {
    return value === "foo" || value=="";
}, "myMessage");

However, jQuery Validation Plugin automagically applies an AND logical operator between requirements, how can I have an OR ? 但是,jQuery Validation Plugin自动在需求之间应用AND逻辑运算符,我如何拥有OR? (or have my custom method call jQuery Validation Plugin built-in rules like digits ? I'm not javascript guru, maybe that's obvious looking at the source ?) (或者让我的自定义方法调用jQuery Validation Plugin内置规则,例如数字 ?我不是javascript大师,也许从源代码上看这很明显吗?)

Title: ... custom logical operators (=> rule1 OR rule2 OR rule3…) 标题:... 自定义逻辑运算符(=> rule1或rule2或rule3…)

When you declare rules for this plugin, it will apply ALL rules that are declared using a logical AND. 当您为此插件声明规则时,它将应用所有使用逻辑AND声明的规则。

rules: {
    myField: {
        digits: true,
        //required: false, // <- 'false' is superfluous/default
        maxlength: 9999,
        myCustomRule: true
    }
}

The above states that this field must satisfy digits AND maxlength AND myCustomRule ; 上面指出该字段必须满足digits AND maxlengthmyCustomRule ; AND it will not be required if myCustomRule is properly written. 并且,如果正确编写了myCustomRulerequired

There is no option within this plugin to change "AND" into "OR" when evaluating the list of declared rules. 在评估已声明规则的列表时,此插件中没有选项将“ AND”更改为“ OR”。

The only way you can evaluate the rules using an OR operator is by creating a single custom rule that encompasses all of your validation logic into one function. 您可以使用OR运算符评估规则的唯一方法是创建一个将所有验证逻辑都包含在一个函数中的单个自定义规则。

rules: {
    myField: {
        myCustomRule: true
    }
}

Where myCustomRule contains your own function with all rule logic separated by OR operators. 其中myCustomRule包含您自己的函数,所有规则逻辑均由OR运算符分隔。 See below for the solution to your exact example. 请参见下面的具体示例解决方案。

... how can I define a field that can be blank OR digits OR "foo" ? ...如何定义一个可以为空或数字或“ foo”的字段?

You must create a custom rule that does all three within one function. 必须创建一个自定义规则,以在一个功能中执行所有这三个操作。

  • blank : This is considered "optional" and when you create your own custom rule, it will not allow the field to remain blank unless you compare the result to this.optional(element) using an OR operator. blank :这被认为是“可选的”,当您创建自己的自定义规则时,除非您使用OR运算符将结果与this.optional(element)进行比较,否则该字段将保持空白。

  • "foo" : Use value === "foo" “ foo” :使用value === "foo"

  • digits : Just use the code for the existing digits rule, /^\\d+$/.test(value) 数字 :只需将代码用于现有digits规则/^\\d+$/.test(value)

Then string it all together using OR operators... 然后使用OR运算符将它们串在一起...

$.validator.addMethod("myCustomRule", function(value, element) {
    return this.optional(element) || value === "foo" || /^\d+$/.test(value);
}, "myMessage");

Everything is contained within this new custom rule, so you must not declare any other rules... 所有内容都包含在此新的自定义规则中,因此您不得声明任何其他规则...

var myValidator = $("#myFormId").validate({
    rules: {
        myField: {
            myCustomRule: true
        }
    }
});

The result is an "optional" field that will also validate for digits or the string "foo". 结果是一个“可选”字段,该字段还将验证数字或字符串“ foo”。

DEMO: jsfiddle.net/tLoef0ov/ 演示: jsfiddle.net/tLoef0ov/

Here is how to do it. 这是怎么做。 You cannot use digits: true in your case since you want it to be either a number or "foo". 您不能使用digits: true在您的情况下为digits: true ,因为您希望它是数字或“ foo”。

 var myValidator = $("#myFormId").validate({ rules: { myField: { required: false, maxlength: 9, myCustomRule: true } } }); $.validator.addMethod("myCustomRule", function(value, element) { return value === "" || value === "foo" || !isNaN(value); }, "myMessage"); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.min.js"></script> <form id="myFormId"> <input type="text" name="myField" /> </form> 

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

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