8

I want users to allow only phone numbers in following format

xxx-xxx-xxxx or xxxxxxxxxx all digits only . Can some one suggest a regular expression to do this ?

Alex Aza
  • 76,499
  • 26
  • 155
  • 134
Pit Digger
  • 9,618
  • 23
  • 78
  • 122
  • 2
    Check out this link: http://tinyurl.com/3wdk4lh –  Jun 01 '11 at 01:56
  • 1
    That seems to be quite simple. Have a look at http://www.regular-expressions.info/ to get started. – Felix Kling Jun 01 '11 at 01:57
  • 3
    This isn't for a public webpage is it? Because people who live in other countries might be a bit annoyed when their phone numbers are rejected. – nnnnnn Jun 01 '11 at 02:11
  • I agree with nnnnnn. In some use cases, though, it would be preferable. I have had clients who only shipped to the U.S. and Canada, so allowing other number formats wouldn't meet their requirements. – g_thom Jun 01 '11 at 02:33
  • When I think phone number validation, I think of more than just length. My fav thing to do when registering a phone number is `911-911-9111` because anything that requires a phone number is lacking. – vol7ron Jun 01 '11 at 02:35

5 Answers5

13

Something like this:

\d{3}-\d{3}-\d{4}|\d{10}
Alex Aza
  • 76,499
  • 26
  • 155
  • 134
10

While general phone number validation is a larger problem than what you're trying to solve, I'd do the following:

var targ=phone_number_to_validate.replace(/[^\d]/g,''); // remove all non-digits
if(targ && targ.length===10) {
  // targ is a valid phone number
}

Doing it this way will validate all of the following forms:

xxxxxxxxxx
xxx-xxx-xxxx
(xxx) xxx-xxxx
etc.

Also, to trivially check for a valid U.S. area code, you can use:

if(targ.matches(/^[2-9]\d{2}/)) // targ is a valid area code

Again, this is a trivial check. For something a little more rigorous, see this List of Legal US Area Codes.

See also A comprehensive regex for phone number validation

Community
  • 1
  • 1
Rob Raisch
  • 17,040
  • 4
  • 48
  • 58
  • +1 I was going to suggest something similar, only you'll want some checks because I think `targ.length` will be an error if it isn't initialized. – vol7ron Jun 01 '11 at 02:33
2

Use this :

/^(1-?)?(([2-9]\d{2})|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/

Pank
  • 13,800
  • 10
  • 32
  • 45
1
var rx=/^\d{3}\-?\d{3}\-?\d{4}$/;
if(rx.test(string)){
//10 diget number with possible area and exhange hyphens
}
else //not
kennebec
  • 102,654
  • 32
  • 106
  • 127
  • This would accept 000-000-0000. I like Pank's better. I would add the `[2-9]` sequence where he did, personally, though I know the requestor said "all digits"... – vapcguy Aug 12 '14 at 01:48
1

The following regular expression can be used to validate defined and operational (as of 6/2011) telephone area codes within the U.S.:

var area_code_RE=
 '(2(?:0[1-35-9]|1[02-9]|2[4589]|3[1469]|4[08]|5[1-46]|6[0279]|7[068]|8[13])'+
 '|3(?:0[1-57-9]|1[02-9]|2[0139]|3[014679]|47|5[12]|6[019]|8[056])'+
 '|4(?:0[126-9]|1[02-579]|2[3-5]|3[0245]|4[023]|6[49]|7[0589]|8[04])'+
 '|5(?:0[1-57-9]|1[0235-8]|[23]0|4[01]|5[179]|6[1-47]|7[01234]|8[056])'+
 '|6(?:0[1-35-9]|1[024-9]|2[0368]|3[016]|4[16]|5[01]|6[012]|7[89]|8[29])'+
 '|7(?:0[1-4678]|1[2-9]|2[047]|3[1247]|4[07]|5[47]|6[02359]|7[02-59]|8[156])'+
 '|8(?:0[1-8]|1[02-8]|28|3[0-25]|4[3578]|5[06-9]|6[02-5]|7[028])'+
 '|9(?:0[1346-9]|1[02-9]|2[0578]|3[15679]|4[0179]|5[124679]|7[1-35689]|8[0459])'+
 ')';
Rob Raisch
  • 17,040
  • 4
  • 48
  • 58