3

Possible Duplicate:
A comprehensive regex for phone number validation

I am pretty close to a full regex that I need. However I can't seem to get an optional white space allowed.

Regex

^(\(?\d{3}\)?\-?\d{3}\-?\d{4})$

Requirements - Only these four

111-111-1111 - Works

(111)111-1111 - Works

(111) 111-111 - Doesn't Capture

1111111111 - Works

How do I add an optional space after the parenthesis?

Community
  • 1
  • 1
Adam
  • 3,615
  • 6
  • 32
  • 51
  • What I usually do is strip out non-numeric characters, and make sure the length is 10. I then format the 10 digit string to how I believe it should be in the database. – Matthew Oct 01 '12 at 19:14
  • I have to conform to the strict rules I was given for only the 4 formats shown. – Adam Oct 01 '12 at 19:16
  • Escaping the parenthesis? Putting parenthesis around the whole thing? Why? Sorry, but to me, that's an ugly regex. Here's my version: `/^[(]?\d{3}[)]?[-\s]?\d{3}-?\d{4}$/` . The part you wanted was `[-\s]?` after the closing parenthesis, not `\-?`, where the hyphen did not need escaping, anyway. – vapcguy Aug 14 '14 at 02:13

4 Answers4

5
^(\(?\d{3}\)*\s?\-?\d{3}\-?\d{4})$

The *\s means 0 or 1 white space characters.

EDIT: Actually, I like this version better because it allows either the dash or the space, but not both. Also, ? means 0 or 1, * means 0 or more:

^(\(?\d{3}\)?[\s,\-]?\d{3}\-?\d{4})$
Michael Sallmen
  • 728
  • 5
  • 13
  • When using http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx that still doesn't capture the one with the space after the parenthesis? – Adam Oct 01 '12 at 19:09
  • Strange. I just tried it and it worked at your link. Here's the tool I originally used: http://regexhero.net/tester/ – Michael Sallmen Oct 01 '12 at 19:16
  • Yeah I just re-typed them and that worked. Very odd I am not sure what was happening the first time. – Adam Oct 01 '12 at 19:18
4

How about \(?\b[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}\b which matches 3334445555, 333.444.5555, 333-444-5555, 333 444 5555, (333) 444 5555 and all combinations thereof.

Updated

You're running into the limitations of REGEX and so the ugly solution really is:

(\d{3}-\d{3}-\d{4}|\(\d{3}\)\s?\d{3}-\d{4}|\d{10})

Example

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • I cannot allow all the other combinations. Just the ones I listed. – Adam Oct 01 '12 at 19:08
  • I don't think it's possible due the fact that you want to allow a space in the same place as a dash, but only if it's after a ) which also requires a look back at another (. – Erik Philips Oct 01 '12 at 19:10
  • That second one you posted seems to work pretty well. Why do you seem so against it with all the ugly talk lol – Adam Oct 01 '12 at 19:26
  • It's basically saying try this, if it doesn't work, try this, if it doesn't work try this... my personal preference is to reduce the complexities of anything which needs to test multiple times to find a match (regardless of the actual use, regex, enums over reflection, etc). – Erik Philips Oct 01 '12 at 19:31
  • Does this change at all to use in javascript? ex: var re = new RegExp("(\d{3}-\d{3}-\d{4}|\(\d{3}\)\s?\d{3}-\d{4}|\d{10})"); valid = re.test(value); – Adam Oct 01 '12 at 22:34
1

Somtimes is better to match one of three whole expressions:

^(\d{11}|(\d{3}-){2}\d4|\(\d{3}\) ?\d\d\d-\d\d\d)$

rbernabe
  • 1,062
  • 8
  • 10
0

I would not deal with the regex based on the formatting, but rather the numbers entered in. This question shows an answer you should likely adopt:

A comprehensive regex for phone number validation

Community
  • 1
  • 1
seangates
  • 1,467
  • 11
  • 28