简体   繁体   English

jQuery验证引擎必需阻止了我的自定义必需验证器

[英]jQuery validation engine required blocks my custom required validator

I have a form with 2 text inputs and 2 radio buttons. 我有一个带有2个文本输入和2个单选按钮的表单。 One radio button is when customer wants all future communication to happen via paper mail the other button is when customer wants to communicate via email. 一个单选按钮是客户希望以后通过纸质邮件进行所有通信时,另一个单选按钮是客户希望通过电子邮件进行通信时。

When user chooses email then user needs to fill one of the input textboxes with his email address. 当用户选择电子邮件时,则需要用其电子邮件地址填写输入文本框之一。 When user chooses postal then he has to fill the other box with postal address. 当用户选择邮政时,他必须在另一个框中填写邮政地址。

I have a custom jquery validation engine function for this: 我为此有一个自定义的jquery验证引擎功能:

function checkBillRoutePostalAddressConflict(field, rules, i, options){
  var fieldValue = field.val();
  var accountIdR = field.attr('id').match(/\d+$/);
  var radioButton = jQuery('#postalBillsDestination_' + accountIdR);
  if(!fieldValue && radioButton.attr("checked")){
    alert('options.allrules.checkBillRoutePostalAddressConflict.alertText') // for debugging
    return options.allrules.checkBillRoutePostalAddressConflict.alertText;
  }
}

Validator is added with class to my input text field class="validate[funcCall[checkBillRoutePostalAddressConflict]]" 验证程序随类添加到我的输入文本字段class="validate[funcCall[checkBillRoutePostalAddressConflict]]"

The problem is it doesnt work. 问题是它不起作用。 The debugging alert message shows up which means the conditions are correct. 显示调试警报消息,表示条件正确。 If i replace it with one of my other custom validators then everything works well. 如果我将其替换为其他自定义验证器之一,则一切正常。

Someone told me that if you write a custom validation function to a field that is empty and shouldnt be then the message is not shown because jQuery wants you to use validate[required]. 有人告诉我,如果您将自定义验证函数写入一个空的字段,则不应显示该消息,因为jQuery希望您使用validate [required]。

If this is true, does anybody know some kind of not-too-hacky workaround for this? 如果这是真的,那么有人知道这种方法不是太hacky吗?

One option is to use showPrompt directly, but mabye there is something cleaner and better? 一种选择是直接使用showPrompt ,但是mabye还有更干净更好的东西吗?
Thank you! 谢谢!

Rule for element can be not just true or false, but also a function. 元素规则不仅可以是true或false,还可以是一个函数。 That function could check if email checkbox is checked and return true (false otherwise): 该函数可以检查是否已选中电子邮件复选框,然后返回true(否则返回false):

$('#form').validate({
    'rules': {
        'email_text_field': {
            'required': function() { return $('#email-radio').is(':checked'); }
        }
    }           
});

Here default required validator is used. 这里使用默认的required验证器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM