0

我有一个包含许多字段的表单,这些字段逐渐显示。在显示下一个字段之前,我需要检查字段是否正常,所以我想通了:

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 并继续前进。验证器需要重置还是什么?有没有更简单的方法?

4

2 回答 2

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!")
         }
       }        
    });                   
}
于 2012-05-17T15:36:02.967 回答
0

这不是您添加和删除规则的方式。根据插件的文档

.rules('add', {
        required: true,
        number: true
});


.rules('remove', {
        required: true,
        number: true
});

注意:您必须在使用其中任何一种方法之前.validate()调用表单。

于 2012-05-17T15:30:16.793 回答