我有一个包含许多字段的表单,这些字段会逐步显示。在显示下一个字段之前,我需要检查字段是否正常,所以我想出了这个:
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!")
}
}
});
}
对于第一组,一切都很好(也在调试器上),但对于第二组,它只是验证确定并继续。验证器是否需要重置?有更简单的方法吗?
答案 0 :(得分:0)
这不是您添加和删除规则的方式。根据{{3}},
.rules('add', {
required: true,
number: true
});
.rules('remove', {
required: true,
number: true
});
注意:您必须使用其中任何一种方法在之前的表单上调用.validate()
。
答案 1 :(得分:0)
switch (index){
case '1':
addRules("nom","the Name!!");
break;
case '2':
addRules("commune2","the commune!!");
break;
case '3':
addRules("addresseLivraison","the addresse!!");
break;
}
然后使用此功能:
function addRules(att,text){
$("#contractForm").validate({
rules: {
att: {
required: true,
minlength: 2
}
},
messages: {
att: {
required: text,
minlength: jQuery.format("At least {0} characters required!")
}
}
});
}