我正在尝试以编程方式检查表单的有效性,下面的代码片段在单击按钮“#btnSet”时给出 true,即使我在电子邮件字段中输入无效值,如果我再次单击它,它也会给出 false .
$.validator.addMethod(
"regex",
function(value, element, regexp) {
var re = new RegExp(regexp);
return this.optional(element) || re.test(value);
},
"Please check your input."
);
var validator = $("#myForm").validate({
rules:{
cen:"email",
frm:{email:true,required:true},
sub:{required:true, minLength:3,maxLength:50},
fn:{required:true,minLength:3,maxLength:50},
forCols:{regex:"^[1-9]?[1-9](,[1-9]?[1-9])*$"},
ftrCols:{regex:"^[1-9]?[1-9](,[1-9]?[1-9])*$"}
}
});
$("#btnSet").on("click",(function(validator){
return function(){
alert(validator.valid());
if(!validator.valid()){
validator.showErrors();
return;
}
}
}(validator)));
如果我用这个函数替换点击事件处理程序
$("#btnSet").on("click",function(){
alert($("#myForm").valid());
if(!$("#myForm").valid()){
return;
}
});
报错如下
a.validator.methods[d] is undefined
最佳答案
a.validator.methods[d] is undefined
意味着你有未定义的方法(规则);换句话说,规则无效...
sub:{required:true, minLength:3,maxLength:50},
在这种情况下,您的规则拼写不正确。它不是 minLength
和 maxLength
,而是 minlength
和 maxlength
...
....
sub: {
required: true,
minlength: 3,
maxlength: 50
},
....
关于javascript - jQuery 验证 - .valid 方法第一次尝试验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43368370/