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?
How about
\(?\b[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}\bwhich matches3334445555,333.444.5555,333-444-5555,333 444 5555,(333) 444 5555and all combinations thereof.Updated
You’re running into the limitations of REGEX and so the ugly solution really is:
Example