6

Possible Duplicate:
US Phone Number Verification

I need to validate US phone number. It could be in the format:

xxx-xxx-xxxx
(xxx) xxx xxxx
(xxx)-xxx-xxxx
xxxxxxxxxx

but it should not be

xxx-xxx-xxxx-
-xxx-xxx-xxxx

It should accept digits, hyphens, space and parentheses.

Currently I use

^\[0-9 \-\. ]+$ 

which does not validate dash at the beginning or end.

Community
  • 1
  • 1
Sandeep
  • 63
  • 1
  • 1
  • 3

5 Answers5

7
^\(?\d{3}\)?[- ]?\d{3}[- ]?\d{4}$
navid
  • 566
  • 6
  • 15
staafl
  • 3,147
  • 1
  • 28
  • 23
  • what you have this number : xxx)-xxx-xxx is it valid? – Eugene Aug 23 '12 at 23:00
  • 2
    or this: `xxx) xxx-xxxx` or this: `xxxxxx xxxx` – Mihai Toader Aug 23 '12 at 23:01
  • 1
    i don't see why not. if the poster disagrees, i will be happy to edit my answer to rule such cases out, but as it is, the rx satisfies the requirements. – staafl Aug 23 '12 at 23:03
  • Thanks Staafi for the quick response. Unfortunately this code is throwing an error : Uncaught SyntaxError: Invalid regular expression. – Sandeep Aug 23 '12 at 23:04
  • the regex was tested at http://regexpal.com/ and works correctly... please post a code snippet so i can see what the problem is – staafl Aug 23 '12 at 23:10
  • It works fine when I checked in online regex validator. But I use it with jquery validator. Code is like this. Phone: { required: true, regex: "^\(?\d{3}\)?[- ]?\d{3}[- ]?\d{4}$"}, – Sandeep Aug 23 '12 at 23:14
  • you are missing the \ before the opening and closing parentheses in the beginning – staafl Aug 23 '12 at 23:16
  • I think it was stripped off when I copied. I tried exactly the same regex. – Sandeep Aug 23 '12 at 23:21
  • well, whatever the problem is, it's not in my submission, and i'm afraid i can't help you any further as it is – staafl Aug 23 '12 at 23:23
  • This solutions works. when I added it in a function and passed that function as jQuery validator rule it works. Thanks for the help. – Sandeep Aug 23 '12 at 23:49
  • this solution with some UI masking works perfect! – Suresh Kaushik Apr 17 '19 at 17:10
4

Well my idea is (after some searching) not new at all! Look at this:

A comprehensive regex for phone number validation

This is an Excellent suggestion btw.

Community
  • 1
  • 1
Eugene
  • 117,005
  • 15
  • 201
  • 306
2

This one is probably correct (assuming some parsing errors depending on the regex engine you are using. It's also ugly as hell :(.

(?:\d{3}(?:\d{7}|\-\d{3}\-\d{4}))|(?:\(\d{3}\)(?:\-\d{3}\-)|(?: \d{3} )\d{4})
Mihai Toader
  • 12,041
  • 1
  • 29
  • 33
  • I will not even try to understand this :) +1 for the work, still. – Eugene Aug 23 '12 at 23:09
  • it's basically trying first `xxxxxxxxxx` and then `xxx-xxx-xxxx` and if that fails it will try `(xxx)-xxx-xxxx` and then `(xxx) xxx xxxx`. But the escaping and parenthesis mess it up. – Mihai Toader Aug 23 '12 at 23:12
  • aha! great idea, validation through exclusion. I wonder if this common problem has a 'universal' library solver. – Eugene Aug 23 '12 at 23:18
2
(^\(?[0-9]{3}\)?\-?\s?[0-9]{3}\-?\s?[0-9]{4}[^-])

I tested this on http://regexhero.net/tester/ and got it to select the following patterns:

xxx-xxx-xxxx 
(xxx) xxx xxxx 
(xxx)-xxx-xxxx
xxxxxxxxxx

It ignored the following patterns:

xxx-xxx-xxxx- 
-xxx-xxx-xxxx

I hope this helps, or at least moves you in the correct direction.

Nick
  • 4,302
  • 2
  • 24
  • 38
0

This should do the trick:

/^([\d]{6}|((\([\d]{3}\)|[\d]{3})( [\d]{3} |-[\d]{3}-)))[\d]{4}$/
  • It starts by checking if the first 6 digits are xxxxxx,
  • if not it looks if the first three digits are (xxx) or just xxx
    • and if one of those it checks if the next three are xxx or -xxx-
  • At the end it checks that there are four trailing digits xxxx
Krycke
  • 3,106
  • 1
  • 17
  • 21