如何扩展 Jquery 验证来验证 3 个所需电话号码中的 2 个?

发布于 2024-09-16 18:41:50 字数 722 浏览 3 评论 0原文

我正在使用 jQuery 验证,这是我用来验证美国电话号码的扩展。

jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
        phone_number = phone_number.replace(/\s+/g, ""); 
        return this.optional(element) || phone_number.length > 9 &&
            phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
    }, "Please specify a valid phone number");

这很好用,但如果我只想验证 3 个数字中的 2 个怎么办?

举个例子,我有

Home Phone: <input name="h" ...>
Work Phone: <input name="w" ...>
Cell Phone: <input name="c" ...>


$('form').validate({
  rules: {
    h: {phoneUS: true},
    w: {phoneUS: true},
    c: {phoneUS: true}
  }
});

I am using jQuery Validation and here is the extension that I am using to validate US phone number.

jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
        phone_number = phone_number.replace(/\s+/g, ""); 
        return this.optional(element) || phone_number.length > 9 &&
            phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
    }, "Please specify a valid phone number");

This works great, but what if I only want to validate 2 of the 3 numbers.

For an example, I have

Home Phone: <input name="h" ...>
Work Phone: <input name="w" ...>
Cell Phone: <input name="c" ...>


$('form').validate({
  rules: {
    h: {phoneUS: true},
    w: {phoneUS: true},
    c: {phoneUS: true}
  }
});

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

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

发布评论

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

评论(2

深陷 2024-09-23 18:41:50

在输入字段中使用类

Home Phone: <input name="h" class="phones">
Work Phone: <input name="w" class="phones">
Cell Phone: <input name="c" class="phones">

并更改您的jquery

$('.phones').validate({
  rules: {
    h: {phoneUS: true},
    w: {phoneUS: true},
    c: {phoneUS: true}
  }
});

use class in input fields

Home Phone: <input name="h" class="phones">
Work Phone: <input name="w" class="phones">
Cell Phone: <input name="c" class="phones">

and change your jquery

$('.phones').validate({
  rules: {
    h: {phoneUS: true},
    w: {phoneUS: true},
    c: {phoneUS: true}
  }
});
风透绣罗衣 2024-09-23 18:41:50

在这里找到答案:

jQuery验证 - 要求至少填写一组中的一个字段

另外,在我发现这一点之前,我确实想出了自己的可行解决方案。

h: {
    phoneUS: true,
    required: function(element) {
        return ($('input.phones:blank').length >= 2);
    }
},
c: {
    phoneUS: true,
    required: function(element) {
        return ($('input.phones:blank').length >= 2);
    }
},
w: {
    phoneUS: true,
    required: function(element) {
        return ($('input.phones:blank').length >= 2);
    }
}

Found an answer here:

jQuery Validate - require at least one field in a group to be filled

Also, before I found this I did come up with my own solution that works.

h: {
    phoneUS: true,
    required: function(element) {
        return ($('input.phones:blank').length >= 2);
    }
},
c: {
    phoneUS: true,
    required: function(element) {
        return ($('input.phones:blank').length >= 2);
    }
},
w: {
    phoneUS: true,
    required: function(element) {
        return ($('input.phones:blank').length >= 2);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文