0

Would like to disable the validation rules for certain fields depending on user input.

Working with jquery validator plugin, is that possible at all? The JSfiddle is here: http://jsfiddle.net/webhelpla/3eQam/

    <html>
    <head>
      <script src="http://code.jquery.com/jquery-latest.js"></script>
      <script type="text/javascript" src="http://jzaefferer.github.com/jquery-validation/jquery.validate.js"></script>

      <script type="text/javascript">
            $(document).ready(function(){
            $("#form").validate();
          });  

       </script> 
    </head>
    <body>


      <form  id="form" method="get" action="">

         <p>
           <label>Validate all
             <input type="radio" name="fields_to_validate" value="all" id="fields_to_validate_0">
             </label>
         </p>    
         <p>         
             <label> Validate name only   
             <input type="radio" name="fields_to_validate" value="name" id="fields_to_validate_1">
             </label>       
         </p>   

        <p>
          <label for="cname">Name</label>
           *<input id="cname" name="name" size="25" class="required" minlength="2" />
         </p>

         <p>
           <label for="cemail">E-Mail</label>
           *<input id="cemail" name="email" size="25"  class="required email" />
         </p>

         <p>
           <input class ="submit" type="submit" value="Submit"/></p></form>
    </body>
    </html>
Stephan Muller
  • 27,018
  • 16
  • 85
  • 126
aaandre
  • 2,502
  • 5
  • 33
  • 46

1 Answers1

1

What youre looking for is a condition argument to the field

I believe it goes like this, remove the hard coded required classes from your input elements, name the two radio buttons denoting your required fields as different names like name_only and:

 $('form').validate({
    rules:
       name:{
        required: function() {
         return $("input:radio[name='nameonly']:checked").val() == 'yes';
       }
     }
  });

so its not determining what should be required by direct user inputs dynamically, its the inputs becoming required based on conditional inputs

Jay Rizzi
  • 4,196
  • 5
  • 42
  • 71
  • Thank you, this is the approach I took. The API is here: http://docs.jquery.com/Plugins/Validation/validate#options (click on the Options tab and scroll down to rules) – aaandre Oct 25 '12 at 17:07