我使用validate()来验证此表单。我使用了addMethod()但由于某种原因,它仍然接受名字中的字母和数字字符。
这就是我所拥有的,我做错了什么?
$(document).ready (function (){
$('#Form').validate({
rules: {
firstN: {
required: true,
minlenght: 1,
alpha: true
},
email: {
required: true,
email: true
}
}
});
$.validator.addMethod("alpha", function(value, element){
return this.optional(element) || value == value.match(/^[a-zA-Z\s]+$/);
}, "Alphabetic characters only");
});
我也尝试过这种方式,我在stackoverflow上找到了它:
$(document).ready (function (){
$('#Form').validate({
rules: {
firstN: {
required: true,
minlenght: 1,
field: {alphaO: "[a-zA-Z]}+"}
},
email: {
required: true,
email: true
}
}
});
$.validator.addMethod("alphaO", function(value, element, theRegEx){
return value.match(new RegExp("^" + theRegEx + "$"));
}, "Alphabetic characters only");
});
答案 0 :(得分:0)
好吧,认为这是一个愚蠢的错字,搞砸了。解决方案实际上有效:
$(document).ready (function (){
$.validator.addMethod("alpha", function(value, element){
return this.optional(element) || value == value.match(/^[a-zA-Z, '']+$/);
}, "Alphabetic characters only please");
$('#Form').validate({
rules: {
firstN: {
alphaOnly: true,
required: true,
minlength: 1
}
}
});
});