jQuery验证插件require字段(如果另一个字段具有值),反之亦然 [英] jQuery validate plugin require field if another field has a value and vice versa

查看:95
本文介绍了jQuery验证插件require字段(如果另一个字段具有值),反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery validate插件来验证我的表单.我想将几对字段绑定在一起,以便如果一个字段具有值,则另一个字段也必须具有一个值.本质上,两个字段(均为文本输入)必须都具有值或均不具有值.有人知道实现此目标的好方法吗?

I am using the jQuery validate plugin to validate my form. I would like to tie several pairs of fields together such that if one field has a value, the other is required to have a value also. Essentially both fields (both text inputs) must either both have a value or both not have a value. Is anyone aware of a good way to accomplish this?

推荐答案

如果您查看文档页面,您可以设置一个depends字段.

if you look at the "rules" section's example code in the documentation page, there is a depends field you can set.

类似于以下内容(这是我的头,未经测试):

something like the following (this is right off my head, not tested):

...
secondInput: {
    required: function(element){
            return $("#firstInput").val()!="";
        }
}
....

这篇关于jQuery验证插件require字段(如果另一个字段具有值),反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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