我正在使用jquery验证插件来验证我的php表单,现在我不得不对一个自定义单词进行验证,我想做的是禁用用户输入两个单词,例如:
word1,word2.
有什么简单的方法吗,我尝试了一些定制的方法,但我不知道我在做什么。有人能给我一个吗?
custom: {
required: true,
words: "word1,word2",
},在我的控制器里
custom: function( value, element ) {
return this.optional(element) || /^-?(?:\word1,word2\)?$/.test(value);
},发布于 2013-08-12 09:38:31
您需要为单词添加一个方法(与‘word:’相同),以声明jqvalidate将使用的自定义函数。这应该在调用jQuery.validate()之前添加;
jQuery.validator.addMethod("words", function(value, element) {
return this.optional(element) || /^/(?!Hello)(?!World)$/.test(value);
}, "Only Hello and World are allowed");参考见验证器AddMethod。不需要控制器代码。
尝试搜索以获得堆栈溢出中的答案。
https://stackoverflow.com/questions/18183662
复制相似问题