简体   繁体   English

jQuery Validate-至少需要一组,再加上其他项

[英]jQuery Validate - Require at least one from group, plus additional items

I'm attempting to use 'jQuery Validate' on a form that requires an email address plus either all items of a shipping address completed or none at all. 我正在尝试在需要电子邮件地址以及送货地址中所有项目均已完成或完全没有的表单上使用“ jQuery Validate”。

Using the sample provided by the solution to this question: jQuery Validate - “Either skip these fields, or fill at least X of them” , I have been able to successfully solve the validation of the address group. 使用该问题的解决方案提供的示例: jQuery Validate-“跳过这些字段,或至少填充其中的X个” ,我就能够成功解决地址组的验证。

The problem, however, is that the logic for validating the email address field does not work. 但是,问题是验证电子邮件地址字段的逻辑不起作用。 From debugging the Validate scripts, the "re-entrant" validation code triggered by calling 'fields.data('being_validated', true).valid();' 通过调试Validate脚本,通过调用“ fields.data('being_validated',true).valid();”触发“重新进入”验证代码。 in the linked example results in a reset of all previously validated errors (ie the email validation error is cleared). 在链接的示例中,所有先前验证的错误都会重置(即,电子邮件验证错误已清除)。

I have modified some existing samples, the first in which removes the offending line and the second with it included. 我已经修改了一些现有示例,第一个示例删除了违规行,第二个示例包含了该行。

Email Validation Working 电子邮件验证工作

Email Validation Fails 电子邮件验证失败

Any tips or suggestions on how to properly solve this or work around the failure? 有关如何正确解决此问题或解决故障的任何提示或建议?

In your case, I'd change a few things around: 在您的情况下,我将更改以下几项内容:

1) set jQuery.validate({ onsubmit: false }) , because you don't want validation to run by default, you want to control when validation runs. 1)设置jQuery.validate({ onsubmit: false }) ,因为您不想默认运行验证,因此您想控制何时运行验证。

2) Fire validation on your own terms during form submit like so: 2)在提交表单期间按您自己的条件进行火灾验证,如下所示:

$("form").submit(function() {
    $("emailField").validate();
    if ($("emailField").valid())
    {
        $("form").validate();
        return $("form").valid();
    }
    return $("emailField").valid();
});

I may not understand your requirements fully, but hopefully this will at least help you look at the solution from a different perspective. 我可能无法完全理解您的要求,但是希望这至少可以帮助您从不同的角度看待解决方案。

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

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