更新时间:2022-05-20 09:17:54
你不能使用相同的 key:value
对两次,因为第二个实例将覆盖第一个。
You cannot use the same key:value
pair twice since the second instance will override the first.
你有几个选项。
将两个正则表达式组合成一个。将使用一条错误消息声明一种方法。因此,您将创建自己的自定义,而不是使用 additional-methods.js
文件中的模式
规则/方法。使用 .addMethod()
方法的规则。
Combine the two regex expressions into one. One method will be declared with one error message. So instead of using the pattern
rule/method from the additional-methods.js
file, you will create your own custom rule using the .addMethod()
method.
不使用正则表达式模式,而是使用 pattern
规则一次并使用 .addMethod()
创建新的第二条规则。
Instead of combining the regex patterns, use the pattern
rule once and create a new second rule using .addMethod()
.
请参阅: http:// jqueryvalidation .org / jQuery.validator.addMethod /
我创建了一个专用于每个正则表达式的自定义方法模式,并给它一个语义相关的名称,类似'电子邮件','电话','字母数字','IP'等。(这也是所有正则表达式评估规则处理相同的方式在这个插件内部。)
I'd create a custom method dedicated to each regex pattern and give it a semantically relevant name, something like 'email', 'phone', 'alphanumeric', 'IP', etc. (This is also the same way all the regex evaluated rules are handled internally by this plugin.)
jQuery.validator.addMethod("foo", function(value, element) {
return this.optional(element) || /^[A-Za-z0-9\w]{4,20}/.test(value);
}, "Your entered data is not foo");
jQuery.validator.addMethod("bar", function(value, element) {
return this.optional(element) || /^[\d\w\xC4\xD6\xDC\xE4\xF6\xFC\xDF]*$/.test(value);
}, "Your entered data is not bar");
声明如下......
Declared like this...
"password": {
required: true,
foo: true,
bar: true
}