javascript 国际电话号码的Javascript正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4147614/
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
Javascript Regular Expression for International Phone Number
提问by Amen
The following regular expression isn't working for international phone numbers that can allow up to 15 digits:
以下正则表达式不适用于最多允许 15 位数字的国际电话号码:
^[a-zA-Z0-9-().\s]{10,15}$
What needs to be adjusted?
需要调整什么?
回答by BalusC
You may find the following regex more useful, it basically first strips all valid special characters which an international phone numbercan contain (spaces, parens, +, -, ., ext) and then counts if there are at least 7 digits (minimum length for a valid local number).
您可能会发现以下正则表达式更有用,它基本上首先去除国际电话号码可以包含的所有有效特殊字符(空格、括号、+、-、.、ext),然后计算是否至少有 7 位数字(有效本地号码的最小长度)数字)。
function isValidPhonenumber(value) {
return (/^\d{7,}$/).test(value.replace(/[\s()+\-\.]|ext/gi, ''));
}
回答by Pointy
Try adding a backslash:
尝试添加反斜杠:
var unrealisticPhoneNumberRegex = /^[a-zA-Z0-9\-().\s]{10,15}$/;
Now it's still not very useful because you allow an arbitrary number of punctuation characters too. Really, validating a phone number like this — especially if you want it to really work for all possibleinternational phone numbers — is probably a hopeless task. I suggest you go with what @BalusC suggests.
现在它仍然不是很有用,因为您也允许任意数量的标点字符。真的,验证这样的电话号码——尤其是如果你希望它真正适用于所有可能的国际电话号码——可能是一项无望的任务。我建议你按照@BalusC 的建议去做。
回答by The Archetypal Paul
回答by dave singer
and then counts if there are at least 7 digits (minimum length for a valid local number).
然后计算是否至少有 7 位数字(有效本地号码的最小长度)。
The shortest local numbers anywhere in the world are only two or three digits long.
世界上最短的本地号码只有两到三位数。
There are many countries without area codes.
有很多国家没有区号。
There are several well-known places with a 3 digit country code and 4 digit local numbers.
有几个著名的地方有 3 位国家代码和 4 位本地号码。
It may be prudent to drop your limit to 6 or 5; just in case.
将限制降至 6 或 5 可能是谨慎的做法;以防万一。

