javascript - Regular Expression for a phone number excluding special characters - Stack Overflow

I'm using a regular expression for a phone number. It allows at least 10 digits including special

I'm using a regular expression for a phone number. It allows at least 10 digits including special characters like +()-

+1(33)-489256

The Regular expression I am using is:

^\D*(?:\d\D*){10,}$

It works OK but it should not allow other special characters in the phone number like #@$%

Please let me know how I can update my regex.

I'm using a regular expression for a phone number. It allows at least 10 digits including special characters like +()-

+1(33)-489256

The Regular expression I am using is:

^\D*(?:\d\D*){10,}$

It works OK but it should not allow other special characters in the phone number like #@$%

Please let me know how I can update my regex.

Share edited Oct 19, 2015 at 6:29 Mariano 6,5214 gold badges33 silver badges48 bronze badges asked Oct 19, 2015 at 6:04 theITvideostheITvideos 1,5022 gold badges19 silver badges29 bronze badges 1
  • Can you add all possible valid formats of the phone numbers – Tushar Commented Oct 19, 2015 at 6:58
Add a ment  | 

2 Answers 2

Reset to default 3
^\D*(?:\d\D*){10,}$
         ^^
       [+()-]

just point out your regex problem

\D: any characters except digits

The problem in your regex is \D*, this will match any non-digit characters(including special characters) any number of times.

Use

/^(\+\d{1,4})?(\d{2}\)-\d{6}$/

Regex Demo and Explanation

  1. /: Delimiters of regex literal
  2. ^: Starts with anchor
  3. \+\d{1,3}: One to three digits after +
  4. \d: Matches single digit
  5. \(: Matches ( literally
  6. \d{2}: Matches exactly two digits
  7. \): Matches ) literally
  8. -: Matches - literally
  9. \d{6}: Matches exactly six digits
  10. $: Ends with anchor

Live Demo

input:valid {
  color: green;
}
input:invalid {
  color: red;
}
<input type="text" pattern="(\+\d{1,4})?\(\d{2}\)-\d{6}" />

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745073075a4609670.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信