老问题,但我正在添加我的答案,以便获得帮助
可以使用以下内容:
创建一个函数来封装我们的验证测试,具有以下签名:
function mytest(value, element, params){...}
建立函数后,我们可以将其附加到 jQuery Validation 插件。为此,我们调用验证器对象的 addMethod() 函数。
我的代码:
$(document).ready(function() {
// other code
// :
// :
$.validator.addMethod(
"passwd",
function(value, element, regexp) {
var re = new RegExp(regexp);
return this.optional(element) || re.test(value);
},
"Invalid input"
);
$("#add_user_form").validate({
rules:{
user_name: {
required: true,
minlength: 5,
maxlength: 15,
noSpace: true
},
password: {
required: true,
minlength: 8,
passwd: "((?=(.*\\d.*){2,})(?=(.*[a-zA-Z].*){2,})(?=(.*[@#$(){}!~,.!^?/|+=-_%].*){2,}).{8,20})"
}
},
messages:{
user_name: {
required: "*",
minlength: " at least 5 characters",
maxlenght: " only 15 characters",
noSpace: " No space Please"
},
password: {
required: "*",
minlength: " Your password must be at least 8 characters long",
passwd: " Invalid Password choice"
}
});
//
// more code in document.ready
好链接:
Starting With jQuery - How to Write Custom Validation Rules
How to add a rule for regular expression validation?