简体   繁体   English

用于最小和最大长度检查的正则表达式用于电话号码验证

[英]Regex for phone number validation with min and max length check

I need a simple javascript regex to validate phone numbers. 我需要一个简单的javascript正则表达式来验证电话号码。 Only digits, "+" at the beginning and "-" 仅数字,“ +”开头,“-”
Max and min length of digits should be 10 - 12 位数的最大和最小长度应为10-12

The next phones should be allowed: 应该允许使用下一个电话:
1. +380631505624 1. +380631505624
2. 0631505624 2. 0631505624
3. 063-15-05-624 3. 063-15-05-624

The next phones should be NOT allowed: 不允许使用下一部手机:
1. +38063+1505624 1. + 38063 + 1505624
2. asd0631505624 2. asd0631505624
3. -063-15-05-624- 3. -063-15-05-624-
4. +0---3-1--24 4. +0 --- 3-1--24

The Comment of Quentin is the way to go , but a Regex - Expression for your needs could be: Quentin的Comment是必经之路 ,但是正则表达式-您所需的表达方式可能是:

^\+?\d+(\d\-|\d)+\d$

I hope it helps, or is atleast a point in the right direction 我希望它会有所帮助,或者至少是正确的方向

It all depends on how save/strict you want to check 这完全取决于您要检查/保存的方式

BTW.: is javascript ist would look something like this 顺便说一句:是javascript ist会看起来像这样

 ...
 if (/^\+?(\d\-|\d)+\d$/ig.test("+43595-995-995")){
    alert("GOOD");
 }
 ...

Here you can find a working demo( On JsFiddler ) tested wit Chrome 24+ (with all numbers of your question) 在这里,您可以找到经过Chrome 24+测试的工作演示版( 在JsFiddler上 )(包含所有问题的编号)

In above code there has no any lenght validation, so I am just correcting the above answer 在上面的代码中没有任何长度的验证,所以我只是在纠正上面的答案

http://jsfiddle.net/KRwHQ/4/ http://jsfiddle.net/KRwHQ/4/

 if (/^\+?((\d\-|\d)+\d){4,}$/ig.test("+43595-995-995")){
     alert("GOOD");
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM