views:

112

answers:

2

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?

+1  A: 

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"
})
gmcalab
Cool .. it worked
shake
A: 

Does decimal:true actually work? Shouldn't it be number:true?

GeneralCS
You are right .. i dont remember whether I wrote it myself or got it from somewhere but I have a custom function for validating decimals: jQuery.validator.addMethod("decimal", function(value, element) { return this.optional(element) || /^\d+(\.\d+)?$/.test(value);});
shake