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:
1-234-567-89011-234-567-8901 x12341-234-567-8901 ext12341 (234) 567-89011.234.567.89011/234/567/890112345678901
I’ll answer with my current attempt, but I’m hoping somebody has something better and/or more elegant.
Better option… just strip all non-digit characters on input (except ‘x’ and leading ‘+’ signs), taking care because of the British tendency to write numbers in the non-standard form
+44 (0) ...when asked to use the international prefix (in that specific case, you should discard the(0)entirely).Then, you end up with values like:
Then when you display, reformat to your hearts content. e.g.