jQuery 验证:如果填充了另一个输入,则只需要输入

2023-12-01

我在用jQuery 验证插件来验证“配置页面”。我有很多输入(如姓名、电话、电子邮件...),最后是“更改密码”部分。

我有 3 个输入,例如:

<label>Current password:</label>
<input name="current_password" type="password" id="current_password">

<label>New password:</label>
<input name="new_password" type="password" id="new_password">

<label>Confirm new password:</label>
<input name="confirm_new_password" type="password" id="confirm_new_password">

我要那个:

If [当前密码 OR 新密码 OR 确认新密码] 未填写,也不是必需的。 (因此用户将“提交”页面而不更改其密码)。

但如果其中一项已填满,则所有项均需填写。

我怎样才能做到这一点?


I saw a 类似的问题但答案在这里不起作用,即使进行了一些更改,例如:

required: function(element){ return $('#new_password').val()!="" },

Try

var $cp = $('#current_password'),
    $np = $('#new_password'),
    $cnp = $('#confirm_new_password');

then

{
    rules: {
        current_password: {
            required: function () {
                return $np.val().length > 0 || $cnp.val().length > 0
            }
        },
        new_password: {
            required: function () {
                return $cp.val().length > 0 || $cnp.val().length > 0
            }
        },
        confirm_new_password: {
            required: function () {
                return $cp.val().length > 0 || $np.val().length > 0
            }
        }
    },
}

Demo: Fiddle

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

jQuery 验证:如果填充了另一个输入,则只需要输入 的相关文章

随机推荐