我正在使用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 或 new_password 或 confirm_new_password ],则不需要它们。 (因此用户可以在不更改密码的情况下提交页面。)
但如果其中一个被填满,那么所有这些都是必需的。
我该怎么做?
我看到similar question,但答案在这里没有用,即使有一些变化,例如:
required: function(element){ return $('#new_password').val()!="" },
答案 0 :(得分:2)
尝试
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