FAQ
overflow

Great Answers to
Questions About Everything

QUESTION

I'm trying to put together a comprehensive regex to validate phone numbers. Ideally it would handle international formats, but it must handle US formats, including the following:

I'll answer with my current attempt, but I'm hoping somebody has something better and/or more elegant.

{ asked by Nicholas Trandem }

ANSWER

Better option... just strip all non-digit characters on input (except 'x').

then, you end up with values like:

 12345678901
 12345678901x1234
 345678901x1234
 12344678901
 12345678901
 12345678901
 12345678901

Then when you display, reformat to your hearts content. e.g.

  1 (234) 567-8901
  1 (234) 567-8901 x1234

{ answered by scunliffe }
Tweet