How to avoid validation for hide fields through jquery validate?

I have one dropdown with condition like Required, Option and hidden for around 8 text fields. Few fields are getting validation and throwing error message even fields are hiding. How to avoid this? Please help on this.

My code is:

            function responseFunction(res) {
    //1:mandatory
    //2:optional
    //3:hidden
    var str = res.split("~");
   for (i in str)
    {
     var field= str[i].split(':');
            switch (field[0]) {
            case "BillingAddressLine1": 
                 switch (field[1]) {                    
                  case "1":$('#ContentSection_lblBillingInfoAddress').prepend('<span>*</span>');
                  $("#<%= txtBillingInfoAddress.ClientID %>").rules("add", {required  : true, messages : {
                  required    : 'Please Enter Address'   }});
                  break;
                  case "3":$('#ContentSection_lblBillingInfoAddress, #ContentSection_txtBillingInfoAddress').hide().parent('p').css("paddingTop", "0px");
                  break;
                 }
            break;
            case "BillingFullName": 
                 switch (field[1]) {
                  case "1":$('#ContentSection_lblBillingInfoAccountHolderName').prepend('<span>*</span>');
                   $("#<%= txtBillingInfoAccountHolderName.ClientID %>").rules("add", {required  : true, messages : {
                  required    : 'Please Enter Account Holder Name'   }});
                  break;
                  case "3":$('#ContentSection_lblBillingInfoAccountHolderName, #ContentSection_txtBillingInfoAccountHolderName').hide().parent('p').css("paddingTop", "0px");
                  break;
                 }
            break;
    }
    }

My screenshot is: https://www.dropbox.com/sh/asmpnuiqqlo40us/S9yHCuNSyl?m#f:errorThrowing.jpg

Upvotes: 1

Views: 1278

Answers (4)

Mark
Mark

Reputation: 218

Hide Validation from a javascript method, something like :

function lalala()
{
  var validator1 = document.getElementById('Validator1ClientID');
  ValidatorEnable(validator1, false); 
}

Upvotes: 1

Vishal
Vishal

Reputation: 1234

add a class attribute to all the hidden elements and use the ignore property to ignore all those elements:

<input type='hidden' class='hiddenClass' />

$('form').validate({
 ignore: '.hiddenClass'
});

Upvotes: 2

Jitendra Pancholi
Jitendra Pancholi

Reputation: 7562

Just put a check in the case statements like below

var control = $("#<%= txtBillingInfoAddress.ClientID %>");
if($(control).is(':hidden')){
  // remove validation
}

Upvotes: 1

karaxuna
karaxuna

Reputation: 26940

Remove field validation when hidden

$('#targetId').rules('remove');

Re add validation when visible

$('#targetId').rules('add', {
    required: true
});

Upvotes: 1

Related Questions