jQuery 验证 - 需要组中至少一项,以及其他项目

发布于 2024-09-03 15:08:40 字数 679 浏览 1 评论 0原文

我正在尝试在需要电子邮件地址的表单上使用“jQuery Validate”,并且需要填写送货地址的所有项目或根本不填写。

使用此问题的解决方案提供的示例: jQuery Validate - “要么跳过这些字段,要么填写至少 X 个字段”,我已经能够成功解决地址组的验证。

然而,问题在于验证电子邮件地址字段的逻辑不起作用。通过调试验证脚本,通过调用 'fields.data('being_validated', true).valid();' 触发“可重入”验证代码链接示例中的结果会重置所有先前验证的错误(即清除电子邮件验证错误)。

我修改了一些现有的示例,第一个示例删除了有问题的行,第二个示例包含了该示例。

电子邮件验证工作

电子邮件验证失败

关于如何正确解决此问题或解决失败问题有任何提示或建议吗?

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.

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.

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();' in the linked example results in a reset of all previously validated errors (i.e. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扭转时空 2024-09-10 15:08:40

在你的情况下,我会改变一些事情:

1)设置jQuery.validate({ onsubmit: false }),因为你不希望验证默认运行,你想控制当验证运行时。

2)在表单提交期间按照您自己的条件进行防火验证,如下所示:

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

我可能不完全理解您的要求,但希望这至少可以帮助您从不同的角度看待解决方案。

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.

2) Fire validation on your own terms during form submit like so:

$("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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文