1

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

3
  • 1
    "Please find my screenshot." - Where did you last see it? Try retracing your steps... (OK, sorry, just kidding. Perhaps you could show your code though?) Commented Aug 17, 2012 at 7:26
  • 1
    no screenshot here...!!! Commented Aug 17, 2012 at 7:31
  • @Sivasenthil: which validation engine you are using? Commented Aug 17, 2012 at 7:34

4 Answers 4

2

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'
});
Sign up to request clarification or add additional context in comments.

4 Comments

Your answer is according to which validation engine/library?
@nnnnn, Vishal: I added my screenshot
@Jitendra: I am using Jquery Validate plugin
@JitendraPancholi answer is as per jQuery validation plugin.
1

Just put a check in the case statements like below

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

Comments

1

Remove field validation when hidden

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

Re add validation when visible

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

Comments

1

Hide Validation from a javascript method, something like :

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.