验证基于其他字段的表单字段 [英] validate form fields based on other fields jquery validate

查看:121
本文介绍了验证基于其他字段的表单字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个输入字段: 姓名, 年龄, 国家

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

我想检查NAME是否是任何东西.如果NAME包含一个值,则需要另外两个字段.如果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.

jquery可以验证吗?

Is this possible with jquery validate?

我当前的验证基于类名,然后在我的js文件中得到了以下内容:

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();
        }

推荐答案

您可以使用依赖项,例如:

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

请参阅: jquery验证

这篇关于验证基于其他字段的表单字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆