ffffff01
ffffff01

Reputation: 5238

validate form fields based on other fields jquery validate

I've got three input fields: NAME, AGE, COUNTRY

I want to check if NAME is anything. If NAME contains a value then the two other fields is required. If NAME is empty they can also skip AGE and COUNTRY.

Is this possible with jquery validate?

My current validation is based on a classname and then I've got the following in my js file:

beforeSubmit: function () {
            return $('#ajaxform').validate().form();
        }

Upvotes: 0

Views: 2740

Answers (1)

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

you can use depends, like:

...
ageInput: {
    required: {
        depends: function(element){
            return $("#nameElement").val()!=""
        }
    }
},
countryInput: {
    required: {
        depends: function(element){
            return $("#nameElement").val()!=""
        }
    }
}

See: jquery Validate

Upvotes: 1

Related Questions