如何在 jQuery.validation 中添加 Not Equal To 规则 [英] How to add a Not Equal To rule in jQuery.validation
本文介绍了如何在 jQuery.validation 中添加 Not Equal To 规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如何制作它以便我可以制定一个字段不等于值的规则.就像我有一个名为 'name' 的字段,所以我不想要 'name' = 'Your Name.'
I was wondering how to make it so that I could make a rule where a field is not equal to a value. Like I have a field called 'name' so I don't want 'name' = 'Your Name.'
有人知道如何做到这一点吗?感谢您的帮助.
Does anybody have an idea of how to do this? thanks for any help.
推荐答案
您可以使用自定义方法,如下所示:
You could use a custom method, something like this:
jQuery.validator.addMethod("notEqual", function(value, element, param) {
return this.optional(element) || value != param;
}, "Please specify a different (non-default) value");
然后像这样使用它:
$("form").validate({
rules: {
nameField: { notEqual: "Your Name" }
}
});
将其添加为这样的规则使其更具扩展性,因此您可以使用它来与其他字段中的默认值进行比较.
Adding it as a rule like this makes it more extensible, so you can use it to compare against the default value in other fields.
这篇关于如何在 jQuery.validation 中添加 Not Equal To 规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文