Got some hidden fields in a form which needs validation(at some later point say, when made visible)? Lets learn how to remove or add validation rules dynamically using jQuery Validate plugin.

We can use rules() method of jQuery-validate plugin to add and remove rules.

Here is how you can remove rules from any element

  rules( 'remove', rules );

remove is a string, specified for removing the rules and rules are the space separated names of rules. This argument is optional, if left unspecified, it removes all the static rules.

Example:

  // removes all the rules
  $('.classOfSelector').rules( 'remove' );
  // removes only `required` and `min` rules from the element
  $('.classOfSelector').rules( 'remove', required min );

Adding rules to the element is also quite simple

 rules( 'add' , rules);

add is a string, specified for adding the rules and rules are the rules to add. These rules are specified in the same format as the rules-option of the validate-method.

Example:

  $( ".classOfSelector" ).rules( "add", { required: true });

Working Example: https://jsfiddle.net/lavika/vtme816s/

References: jQuery-validate()