使用 JavaScript 验证电话号码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4338267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Validate phone number with JavaScript
提问by Castro Roy
I found this code in some website, and it works perfectly. It validates that the phone number is in one of these formats:
(123) 456-7890or 123-456-7890
我在一些网站上找到了这段代码,它运行良好。它验证电话号码是否采用以下格式之一:
(123) 456-7890或123-456-7890
The problem is that my client (I don't know why, maybe client stuffs) wants to add another format, the ten numbers consecutively, something like this: 1234567890.
问题是我的客户(我不知道为什么,也许是客户的东西)想要添加另一种格式,连续十个数字,如下所示:1234567890。
I'm using this regular expression,
我正在使用这个正则表达式,
/^(\()?\d{3}(\))?(-|\s)?\d{3}(-|\s)\d{4}$/
How can I add that it also validates the another format? I'm not good with regular expressions.
我如何添加它也验证另一种格式?我不擅长正则表达式。
采纳答案by josh3736
First off, your format validator is obviously only appropriate for NANP(country code +1) numbers. Will your application be used by someone with a phone number from outside North America? If so, you don't want to prevent those people from entering a perfectly valid [international] number.
首先,您的格式验证器显然只适用于NANP(国家代码 +1)数字。拥有北美以外电话号码的人会使用您的应用程序吗?如果是这样,您不想阻止这些人输入完全有效的 [国际] 号码。
Secondly, your validation is incorrect. NANP numbers take the form NXX NXX XXXX
where N
is a digit 2-9 and X
is a digit 0-9. Additionally, area codes and exchanges may not take the form N11
(end with two ones) to avoid confusion with special services exceptnumbers in a non-geographic area code (800, 888, 877, 866, 855, 900) may have a N11
exchange.
其次,您的验证不正确。NANP 数字采用以下形式NXX NXX XXXX
,其中N
是数字 2-9,X
是数字 0-9。此外,N11
为了避免与特殊服务混淆,区号和交换可能不会采用这种形式(以两个结尾),但非地理区号(800、888、877、866、855、900)中的数字可能有N11
交换。
So, your regex will pass the number (123) 123 4566 even though that is not a valid phone number. You can fix that by replacing \d{3}
with [2-9]{1}\d{2}
.
因此,您的正则表达式将传递号码 (123) 123 4566,即使该号码不是有效的电话号码。您可以通过替换\d{3}
为[2-9]{1}\d{2}
.
Finally, I get the feeling you're validating user input in a web browser. Remember that client-side validation is only a convenienceyou provide to the user; you still need to validate all input (again) on the server.
最后,我感觉您正在验证 Web 浏览器中的用户输入。请记住,客户端验证只是您提供给用户的一种便利;您仍然需要(再次)验证服务器上的所有输入。
TL;DRdon't use a regular expression to validate complex real-world data like phone numbersor URLs. Use a specialized library.
TL;DR不使用正则表达式来验证复杂的现实世界数据,如电话号码或URL。使用专门的库。
回答by EeeeeK
My regex of choice is:
我选择的正则表达式是:
/^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/im
Valid formats:
有效格式:
(123) 456-7890
(123)456-7890
123-456-7890
123.456.7890
1234567890
+31636363634
075-63546725
(123) 456-7890
(123)456-7890
123-456-7890
123.456.7890
1234567890
+31636363634
075-63546725
回答by kennebec
If you are looking for 10 and only 10 digits, ignore everything but the digits-
如果您正在寻找 10 位且只有 10 位数字,请忽略除数字以外的所有内容-
return value.match(/\d/g).length===10;
回答by Paul Schreiber
What I would do is ignore the format and validate the numeric content:
我要做的是忽略格式并验证数字内容:
var originalPhoneNumber = "415-555-1212";
function isValid(p) {
var phoneRe = /^[2-9]\d{2}[2-9]\d{2}\d{4}$/;
var digits = p.replace(/\D/g, "");
return phoneRe.test(digits);
}
回答by Billbad
The following REGEX will validate any of these formats:
以下 REGEX 将验证任何这些格式:
(123) 456-7890
123-456-7890
123.456.7890
1234567890
(123) 456-7890
123-456-7890
123.456.7890
1234567890
/^[(]{0,1}[0-9]{3}[)]{0,1}[-\s\.]{0,1}[0-9]{3}[-\s\.]{0,1}[0-9]{4}$/
回答by elishas
/^[+]*[(]{0,1}[0-9]{1,3}[)]{0,1}[-\s\./0-9]*$/g
(123) 456-7890
+(123) 456-7890
+(123)-456-7890
+(123) - 456-7890
+(123) - 456-78-90
123-456-7890
123.456.7890
1234567890
+31636363634
075-63546725
(123)456-7890
+(123)456-7890
+(123)-456-7890
+(123) - 456-7890
+(123) - 456-78-90
123-456-7890
123.456.7890
1234567890
31636363634
075-63546725
This is a very loose option and I prefer to keep it this way, mostly I use it in registration forms where the users need to add their phone number. Usually users have trouble with forms that enforce strict formatting rules, I prefer user to fill in the number and the format it in the display or before saving it to the database. http://regexr.com/3c53v
这是一个非常宽松的选项,我更喜欢保持这种方式,主要是我在用户需要添加电话号码的注册表中使用它。通常用户对强制执行严格格式规则的表单有问题,我更喜欢用户在显示中或在将其保存到数据库之前填写数字和格式。 http://regexr.com/3c53v
回答by Frawel
Where str could be any of these formarts: 555-555-5555 (555)555-5555 (555) 555-5555 555 555 5555 5555555555 1 555 555 5555
其中 str 可以是这些格式中的任何一个: 555-555-5555 (555)555-5555 (555) 555-5555 555 555 5555 5555555555 1 555 555 5555
function telephoneCheck(str) {
var isphone = /^(1\s|1|)?((\(\d{3}\))|\d{3})(\-|\s)?(\d{3})(\-|\s)?(\d{4})$/.test(str);
alert(isphone);
}
telephoneCheck("1 555 555 5555");
回答by 6502
I would suggest using something clearer (especially thinking to who will have to maintain the code)... what about:
我建议使用更清晰的东西(特别是考虑谁必须维护代码)......怎么样:
var formats = "(999)999-9999|999-999-9999|9999999999";
var r = RegExp("^(" +
formats
.replace(/([\(\)])/g, "\")
.replace(/9/g,"\d") +
")$");
where the regexp is built from a clear template ? Adding a new one would then be a no-brainer and may be even the customer itself could be able to do that in a "options" page.
正则表达式是从一个清晰的模板构建的?添加一个新的将是一件轻而易举的事情,甚至客户本身也可以在“选项”页面中做到这一点。
回答by harinimurugan
Try this one - it includes validation for international formats too.
试试这个 - 它也包括对国际格式的验证。
/^[+]?(1\-|1\s|1|\d{3}\-|\d{3}\s|)?((\(\d{3}\))|\d{3})(\-|\s)?(\d{3})(\-|\s)?(\d{4})$/g
/^[+]?(1\-|1\s|1|\d{3}\-|\d{3}\s|)?((\(\d{3}\))|\d{3})(\-|\s)?(\d{3})(\-|\s)?(\d{4})$/g
This regex validates the following format:
此正则表达式验证以下格式:
- (541) 754-3010 Domestic
- +1-541-754-3010 International
- 1-541-754-3010 Dialed in the US
- 001-541-754-3010 Dialed from Germany
- 191 541 754 3010 Dialed from France
- (541) 754-3010 国内
- +1-541-754-3010 国际
- 1-541-754-3010 在美国拨打
- 001-541-754-3010 从德国拨打
- 191 541 754 3010 从法国拨打
回答by Flavio Ferreira
/^\+?1?\s*?\(?\d{3}(?:\)|[-|\s])?\s*?\d{3}[-|\s]?\d{4}$/
/^\+?1?\s*?\(?\d{3}(?:\)|[-|\s])?\s*?\d{3}[-|\s]?\d{4}$/
Although this post is an old but want to leave my contribuition. these are accepted: 5555555555 555-555-5555 (555)555-5555 1(555)555-5555 1 555 555 5555 1 555-555-5555 1 (555) 555-5555
虽然这个帖子很旧但想留下我的贡献。这些是被接受的: 5555555555 555-555-5555 (555)555-5555 1(555)555-5555 1 555 555 5555 1 555-555-5555 1 (555) 555
these are not accepted:
这些不被接受:
555-5555 -> to accept this use: ^\+?1?\s*?\(?(\d{3})?(?:\)|[-|\s])?\s*?\d{3}[-|\s]?\d{4}$
555-5555 -> 接受此用途: ^\+?1?\s*?\(?(\d{3})?(?:\)|[-|\s])?\s*?\d{3}[-|\s]?\d{4}$
5555555 -> to accept this use: ^\+?1?\s*?\(?(\d{3})?(?:\)|[-|\s])?\s*?\d{3}[-|\s]?\d{4}$
5555555 -> 接受此用途: ^\+?1?\s*?\(?(\d{3})?(?:\)|[-|\s])?\s*?\d{3}[-|\s]?\d{4}$
1 555)555-5555 123**&!!asdf# 55555555 (6505552368) 2 (757) 622-7382 0 (757) 622-7382 -1 (757) 622-7382 2 757 622-7382 10 (757) 622-7382 27576227382 (275)76227382 2(757)6227382 2(757)622-7382 (555)5(55?)-5555
1 555)555-5555 123**&!!asdf# 55555555 (6505552368) 2 (757) 622-7382 0 (757) 622-7382 -1 (757) 622-7382 (6505552368) 2 7) 27 -7382 27576227382 (275)76227382 2(757)6227382 2(757)622-7382 (555)5(55?)-5555
this is the code I used:
这是我使用的代码:
function telephoneCheck(str) {
var patt = new RegExp(/^\+?1?\s*?\(?\d{3}(?:\)|[-|\s])?\s*?\d{3}[-|\s]?\d{4}$/);
return patt.test(str);
}
telephoneCheck("+1 555-555-5555");