validate form fields based on other fields jquery validate

放肆的年华 提交于 2019-12-14 03:12:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!