jQuery disable rule validation on a single field

shake picture shake · May 18, 2010 · Viewed 77.6k times · Source

I am using MVC to create forms that are generated at runtime. For validation, I am trying my hand at the jQuery validation library which is very convenient to use. I have the validation expression of each field in the cdata attribute of the tag

<input type="text" name="xyz" id="xyz" class="defaultTextBox"
  cdata="{validate:{required:true, decimal:true, messages:
          {required:'Please enter an decimal value', 
           decimal:'Please enter a valid decimal'}}}">

This works beautifully. Now one more requirement I have is that some fields are being shown and hidden according to the logic on the page and I need to disable the validation on the hidden fields such that they do not interfere with the form submission. Just toggling the required:true to false and back to true should be enough. Only i do not know how.

Anyone has any experience with that?

Answer

Gabe picture Gabe · May 18, 2010

Just add the ignore rule and define the selector. In this example, the validation will ignore all elements that have the class="ignore"

$("#myform").validate({
   ignore: ".ignore"
})