[英]jquery plugin for international phone number validations
Can you guys suggest a good telephone number validation plugin for jQuery for both US and International numbers? 你们能为美国和国际号码的jQuery建议一个好的电话号码验证插件吗? I Googled a lot but could not find a one that worked well.
我用Google搜索了很多,但找不到一个效果很好的。
It would be great if there was a live validation (ie: it validates instantly when the user types the phone number in the input field) 如果有实时验证会很好(即:当用户在输入字段中键入电话号码时,它会立即验证)
I would suggest using jQuery Validate and then extended the functionality by using the code below, which I found on this blog post . 我建议使用jQuery Validate ,然后使用下面的代码扩展功能,我在这篇博文中找到了。
It offers a nice snippet of code (and it gets better in the comments) which you can use for international numbers as well as US phone numbers. 它提供了一个很好的代码片段(在评论中会更好),您可以将其用于国际号码以及美国电话号码。 I was already using the
additional methods
plugin so I renamed the method intlphone
, and then the code to my web page, after jQuery and before the document.ready
/ validation setup: 我已经在使用
additional methods
插件,所以我重命名了方法intlphone
,然后将代码重命名为我的网页,在jQuery之后和document.ready
/ validation设置之前:
jQuery.validator.addMethod('intlphone', function(value) { return (value.match(/^((\+)?[1-9]{1,2})?([-\s\.])?((\(\d{1,4}\))|\d{1,4})(([-\s\.])?[0-9]{1,12}){1,2}(\s*(ext|x)\s*\.?:?\s*([0-9]+))?$/)); }, 'Please enter a valid phone number');
On the input field I wanted to be a phone number I set the class to be intlphone
and added an HTML5 property / attribute for minimum length (although this could also be done by specifying the validation rules when setting up validation), so my input was something like: 在输入字段我想成为电话号码我将类设置为
intlphone
并添加HTML5属性/属性以获得最小长度(虽然这也可以通过在设置验证时指定验证规则来完成),所以我的输入是就像是:
<input class="required intlphone" name="phone" minLength="7" type="text" />
As the blog post says it accepts digits as well as -+()
and also spaces and extensions in the format x:
or ext:
正如博客文章所说,它接受数字以及
-+()
以及x:
或ext:
格式的空格和扩展ext:
It's not a guarantee that a phone number will be valid 100% of the time but it's not a bad start. 这并不能保证电话号码在100%的时间内都有效,但这不是一个糟糕的开始。
What is the only thing really(!) valid about a phone number? 真正(!)对电话号码有效的唯一事情是什么? At least internationally speaking, there is no such thing as a general format for phone numbers.
至少在国际上,没有电话号码的通用格式。 You can write only digits, use some parentheses, dashes, plus signs etc. But what really matters are the digits.
你只能写数字,使用一些括号,破折号,加号等。但真正重要的是数字。 There is not even a common length restriction in most countries.
大多数国家甚至没有共同的长度限制。
So no, I've not seen a usable phone number validator yet. 所以不,我还没有看到可用的电话号码验证器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.