0

I have a form with many fields which are displayed progressively. I need to check the fields to be OK before I show the next ones so I figured this out:

switch (index){
          case '1': 
              addRules1();
              break;
          case '2':
            addRules2();
            break;
          case '3':
              addRules3();
              break;
          }
          if ($("#contractForm").valid()){
//go to next group of inputs

The addRules functions look like this:

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!")
         }
       }        
    });                                    
} 

For the first group everything is fine (on debugger also) but for the second one it just validates OK and moves on. Does the validator need to be reset or something? is there a easier way?

Samson
  • 2,801
  • 7
  • 37
  • 55

2 Answers2

0

That is not how you would add and remove rules. As per the plugin's documentation,

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


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

Note: you must call .validate() on the form before using either of those methods.

Sparky
  • 98,165
  • 25
  • 199
  • 285
  • I have seen this but this works per element. Can I also use for the entire form? – Samson May 17 '12 at 15:32
  • @radashkRADU, yes you can. As per [the documentation](http://docs.jquery.com/Plugins/Validation/rules#.22add.22rules), for `remove`, if you leave the rules unspecified, it removes _all_ rules. – Sparky May 17 '12 at 15:34
  • 1
    @radashkRADU, also see [this answer](http://stackoverflow.com/questions/8829030/jquery-validation-plugin-adding-rules-that-apply-to-multiple-fields/9056425#9056425) for a bunch of examples showing how to dynamically add rules to multiple fields. – Sparky May 17 '12 at 15:35
  • It doesn't work with IE. I'm using JQuery 1.7.2 and Validate 1.9 – Samson Jun 01 '12 at 13:20
  • @radashkRADU, It's well documented and works perfectly fine in IE 7 and up. You'll have to provide a demo of what you're doing. – Sparky Jun 01 '12 at 15:22
  • Check my last posted question.. I can t get you the link for the moment sorry. – Samson Jun 01 '12 at 15:52
0
 switch (index){
  case '1': 
      addRules("nom","the Name!!");
      break;
  case '2':
     addRules("commune2","the commune!!");
     break;
  case '3':
      addRules("addresseLivraison","the addresse!!");
      break;
  }

then use this function:

function addRules(att,text){
    $("#contractForm").validate({
        rules: {
        att: {
           required: true,
           minlength: 2
         }
       },
       messages: {
           att: {
           required: text,
           minlength: jQuery.format("At least {0} characters required!")
         }
       }        
    });                   
}
M Rostami
  • 4,035
  • 1
  • 35
  • 39
  • I'll test this tomorrow. But would you explain me why this could work and my version not? – Samson May 17 '12 at 19:04
  • your code which you write here has no problem but maybe after these code there was a problem . if you can place your full code in [http://jsfiddle.net](http://jsfiddle.net) to find issue. – M Rostami May 17 '12 at 19:12