如何扩展 Jquery 验证来验证 3 个所需电话号码中的 2 个?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在输入字段中使用类
并更改您的jquery
use class in input fields
and change your jquery
在这里找到答案:
jQuery验证 - 要求至少填写一组中的一个字段
另外,在我发现这一点之前,我确实想出了自己的可行解决方案。
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.