问题
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();
}
回答1:
you can use depends, like:
...
ageInput: {
required: {
depends: function(element){
return $("#nameElement").val()!=""
}
}
},
countryInput: {
required: {
depends: function(element){
return $("#nameElement").val()!=""
}
}
}
See: jquery Validate
来源:https://stackoverflow.com/questions/12405056/validate-form-fields-based-on-other-fields-jquery-validate