我有一个包含许多字段的表单,这些字段逐渐显示。在显示下一个字段之前,我需要检查字段是否正常,所以我想通了:
switch (index){
case '1':
addRules1();
break;
case '2':
addRules2();
break;
case '3':
addRules3();
break;
}
if ($("#contractForm").valid()){
//go to next group of inputs
addRules
函数如下所示:
function addRules1(){
$("#contractForm").validate({
rules: {
nom: {
required: true,
minlength: 2
}
},
messages: {
nom: {
required: "the name!!",
minlength: jQuery.format("At least {0} characters required!")
}
}
});
}
function addRules2(){
$("#contractForm").validate({
rules: {
commune2: {
required: true,
minlength: 2
}
},
messages: {
commune2: {
required: "the commune!!",
minlength: jQuery.format("At least {0} characters required!")
}
}
});
}
function addRules3(){
$("#contractForm").validate({
rules: {
addresseLivraison: {
required: true,
minlength: 2
}
},
messages: {
addresseLivraison: {
required: "the addresse!!",
minlength: jQuery.format("At least {0} characters required!")
}
}
});
}
对于第一组,一切都很好(也在调试器上),但对于第二组,它只是验证 OK 并继续前进。验证器需要重置还是什么?有没有更简单的方法?