admin管理员组文章数量:1355748
I want a very simple Regex for Javascript validation of phone number, which allows 10 digits
and checks min numbers should be 10 and max 12
including - dash two times
for ex. 123-123-1234
I have found some on internet, but none of them is working for min / max length
.
Looking forward for quick response here.
Thanks !
I want a very simple Regex for Javascript validation of phone number, which allows 10 digits
and checks min numbers should be 10 and max 12
including - dash two times
for ex. 123-123-1234
I have found some on internet, but none of them is working for min / max length
.
Looking forward for quick response here.
Thanks !
Share Improve this question edited Mar 12, 2012 at 10:45 XepterX 1,0116 silver badges16 bronze badges asked Mar 12, 2012 at 9:54 Aditya P BhattAditya P Bhatt 22.1k20 gold badges87 silver badges104 bronze badges 3- A good answer to this question should explain that this expression is not possible and suggest workarounds. An excellent answer would provide such an expression. – georg Commented Mar 12, 2012 at 10:02
-
yes but any idea in mind that will work with
min/max length check
n said validation? – Aditya P Bhatt Commented Mar 12, 2012 at 10:08 -
My phone number has 13 characters, one of which is a
+
… be very careful that if you are restricting phone numbers you aren't going to reject real ones used by the people using your site. – Quentin Commented Mar 12, 2012 at 10:40
3 Answers
Reset to default 3You could do this
/^(?!.*-.*-.*-)(?=(?:\d{8,10}$)|(?:(?=.{9,11}$)[^-]*-[^-]*$)|(?:(?=.{10,12}$)[^-]*-[^-]*-[^-]*$) )[\d-]+$/
See it here on Regexr
(?!...)
is a negative lookahead assertion
(?=...)
is a positive lookahead assertion
^ # Start of the string
(?!.*-.*-.*-) # Fails if there are more than 2 dashes
(?=(?:\d{8,10}$) # if only digits to the end, then length 8 to 10
|(?:(?=.{9,11}$)[^-]*-[^-]*$) # if one dash, then length from 9 to 11
|(?:(?=.{10,12}$)
[^-]*-[^-]*-[^-]*$ # if two dashes, then length from 10 to 12
)
)
[\d-]+ # match digits and dashes (your rules are done by the assertions)
$ # the end of the string
What you asking for wouldn't be a simple regular expression and also may be solved without any use 'em.
function isPhoneNumberValid(number){
var parts, len = (
parts = /^\d[\d-]+\d$/g.test(number) && number.split('-'),
parts.length==3 && parts.join('').length
);
return (len>=10 && len<=12)
}
For sure this may be a bit slower than using a piled regex, but overhead is way minor if you not going to check hundreds of thousandths phone numbers this way.
This isn't perfect in any way but may fit your needs, beware however that this allow two dashes in any place excluding start and end of number, so this will return true
for string like 111--123123
.
There's no simple way to do this with regexp, especially if you allow dashes to appear at some different points.
If you allow dashes only at places as in your example, then it would be ^\d{3}-?\d{3}-?\d{4}$
This one: ^[\d-]{10,12}$
matches string with length from 10 to 12 and contains only digits and dashes, but it also will match e.g. -1234567890-
.
本文标签: Regex Javascript Phone number validation min max length checkStack Overflow
版权声明:本文标题:Regex Javascript Phone number validation min max length check - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744000062a2573694.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论