gpt4 book ai didi

jQuery 验证 : input just required if another input is filled

转载 作者:行者123 更新时间:2023-12-01 07:57:03 24 4
gpt4 key购买 nike

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

我有 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">

我想要那个:

如果未填写 [current_password OR new_password OR confirm_new_password],则不需要填写。 (因此用户将“提交”页面而不更改其密码)。

但如果其中一项已填写,则所有这些都是必需的。

我该怎么做?

<小时/>

我看到了similar question但答案在这里不起作用,即使进行了一些更改,例如:

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

最佳答案

尝试

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

然后

{
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
}
}
},
}

演示:Fiddle

关于jQuery 验证 : input just required if another input is filled,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23485348/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com