B_Rox
B_Rox

Reputation: 25

JQuery validation with if conditions

In my form, one of my field is dependent on the value of another field like if it is required or not. I wrote below code but it is not working properly. Please help.

nic: {

    required: function(element){
        if(('#type').val()==3){
            return false;
        }
        else if(('#type').val()==7){
            return false;
        }
        else{
            return true;
        }

},

Upvotes: 1

Views: 65

Answers (3)

Shaminda
Shaminda

Reputation: 343

some times condition might not correct check the value passed to condition. I mean is it 3 or 7 passed to condition

you can check it by

console.log($('#type').val()); to your condition

Upvotes: 1

Anki
Anki

Reputation: 417

Try below code.

nic: {
    isRequired: function(){
        var val = ('#type').val(); 
        if(val == 3 || val == 7){
          return false;
        }
        return true;
},

Upvotes: 1

Shahnewaz
Shahnewaz

Reputation: 360

Try this:

required: function(element){
    if($('#type').val()==3){
        return false;
    }
    else if($('#type').val()==7){
        return false;
    }
    else{
        return true;
    }
}

Upvotes: 0

Related Questions