我为jQuery验证编写了以下简单规则:
jQuery.validator.addMethod("notEqual", function(value, element, param)
{
return this.optional(element) || value != param;
}, "This field is required");
这样可以说:
$("#form").validate({
rules:
{
field1: {notEqual: "Please Select", required: true},
field2: {notEqual: "Year", required: true}
},
messages:
{
field1: {
notEqual: "This field is required",
required: "This field is required"
}
field2: {
notEqual: "This field is required",
required: "This field is required"
}
},
submitHandler: function(form) {
form.submit();
}
});
但是我希望能够说任何一个字段都不允许等于多个事物,例如。
field1: {notEqual: "Please Select" || "Year" || "Some Value", required: true},
但我尝试过这种语法以及安排“ÖR”操作符的其他一些方法,但没有一种方法可行。有人可以帮忙吗?感谢。
答案 0 :(得分:1)
您可以将JavaScript数组作为参数传递给验证方法,如jQuery Validation plugin custom method. Multiple parameters
的答案中所述 例如
field1: {notEqual: ["Please Select", "Year", "Some Value"], required: true},
然后让你的自定义方法像这样
jQuery.validator.addMethod("notEqual", function(value, element, param)
{
var result = value != param[0] && value != param[1] && value != param[2];
return this.optional(element) || result;
}, "This field is required");