Javascript javascript中的手机或电话号码验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8637657/
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
Mobile or phone number validation in javascript
提问by Devashish Dixit
I need to validate phone or mobile number in javascript.
It should match the following types:+91-9198387083
9198387083
09198387083
It is a 10 digit number with one of these four prefixes '+91-'
, '+91'
, '0'
or ''
Can someone suggest me a regex expression in javascript which matches all the three types.
我需要在 javascript 中验证电话或手机号码。
它应该符合以下几种类型:+91-9198387083
9198387083
09198387083
它与这四个前缀的一个10位数字'+91-'
,'+91'
,'0'
或''
可有人建议我在JavaScript中,所有的三种类型相匹配的正则表达式表达。
回答by jfriend00
I'd using something like this:
我会使用这样的东西:
/^(\+91-|\+91|0)?\d{10}$/
This is very specific about one of your three allowable prefixes (or no prefix) followed by exactly 10 digits and no extra characters on the beginning of end of the string.
这对于三个允许的前缀(或无前缀)之一非常具体,后面紧跟 10 位数字,并且在字符串末尾的开头没有额外的字符。
Test app with both valid and invalid phone numbers here: http://jsfiddle.net/jfriend00/K9bjL/
在此处使用有效和无效电话号码测试应用程序:http: //jsfiddle.net/jfriend00/K9bjL/
回答by Alex
Something like this should work:
这样的事情应该工作:
/\+?[0-9]+-?[0-9]/
/\+?[0-9]+-?[0-9]/
回答by jensgram
10 digit number with one of these four prefixes +91-
, +91
, 0
or :
带有以下四个前缀之一的 10 位数字+91-
, +91
,0
或:
/(\+91-?|0)?\d{10}/
回答by DarkAiR
+7 (123) 45-67-890
8(123)381-198-2
and etc.
My simply mask for all of similar phones
我所有类似手机的简单面具
/^[+]?(\d[^\d]*){11}$/