如果失败,如何停止进一步验证



我最近开始使用jquery验证引擎,我想知道:

我只想在另一个字段不为空时才验证一个字段。我已经将 condRequired 验证规则应用于此字段,该规则工作正常,但 condRequired 之后的所有其他规则都会被触发。例如:

<input type="password" id="current-password" name="current-password" placeholder="Current Password" class="validate[condRequired[password],ajax[ajaxValidateUserPassword]]"> 
<input type="password" id="password" name="password" placeholder="New Password" class="ajax[ajaxValidateUserRegistration]]">

如果用户决定要更新其密码,则必须输入当前密码。但是,如果用户输入了新密码,我只想验证当前密码。所以我有:

validate[condRequired[password],ajax[ajaxValidateUserPassword]] 

作为当前密码的验证规则。问题是:即使用户没有输入新密码,

condRequired[password] 

将失败,但是

ajax[ajaxValidateUserPassword]

仍然被解雇。

有没有办法获得

ajax[ajaxValidateUserPassword]

在以下情况下要跳过的验证规则

condRequired[password] 

规则失败?想法是,如果没有输入新密码,我不想打扰对当前密码的任何验证

谢谢!

ValidationEngine 允许您指定使用自定义函数来确定有效性的验证规则。

<input value="" class="validate[required,funcCall[checkHELLO]]" type="text" id="lastname" name="lastname" />
function checkHELLO(field, rules, i, options){
  if (field.val() != "HELLO") {
     // this allows the use of i18 for the error msgs
     return options.allrules.validate2fields.alertText;
  }
}

在该函数中,如果"新密码"不为空,您可以调用任何 ajax 来验证字段

使用选项 "maxErrorsPerField":

$("form").validationEngine({
    maxErrorsPerField: 1
});

相关内容

最新更新