6

I have an input field that I am trying to add custom validation to (required depending on another field). If I put required AND funcCall() I can see that two errors are returned. If I only put the funcCall nothing is returned. I know it's getting in the function and the condition because I did a console.log() but for some reason it seems like it needs an initial rule to fail to show the error.

Call:

<input type="text" class="validate[funcCall[validatePassportRequired]]" id="form_register_passport_number" value="" name="passport_number" size="50">

Function:

function validatePassportRequired(field, rules, i, options) {
  if ($('#register_for').val()!='Local') {
    return options.allrules.required.alertText;
  }
}

So If I change the Call to:

class="validate[required, funcCall[validatePassportRequired]]"  

I get two * This field is required

Do I have to have another validation rule along with the funcCall?

4

2 回答 2

12

只需在返回错误消息之前添加以下行,而不是在返回消息中将函数名称放在 .alertText 之前。

rules.push('required');

@sunzyflower 在您的情况下,您的功能会像这样..

function validatePassportRequired(field, rules, i, options) {
   if ($('#register_for').val()!='Local') {
   rules.push('required');  
   return options.allrules.validatePassportRequired.alertText;
   }
}
于 2013-05-03T22:18:56.240 回答
1

利用

funcCallRequired[validatePassportRequired]

代替

funcCall[validatePassportRequired]

这将在内部添加 required ,而不会出现双重消息。

如果您想了解有关(旧)问题的更多信息:

于 2017-04-27T13:11:37.417 回答