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>

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this is the approach I took. The API is here: docs.jquery.com/Plugins/Validation/validate#options (click on the Options tab and scroll down to rules)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.