Phone Number Validation Regular Expression
Phone number validation can follow the E.164 international format, which is the standard for international telephone numbering. E.164 numbers are formatted with a plus sign (+) followed by the country code and subscriber number, with a maximum of 15 digits total. Phone numbers are a specialized type of number pattern.
International Format (E.164)
Explanation
^- Start of the string.\+?- Optional plus sign (+) for international format.[1-9]- First digit must be 1-9 (country codes cannot start with 0).\d{1,14}- Followed by 1 to 14 additional digits (E.164 allows maximum 15 digits total).$- End of the string.
Implementation
Test Cases
| Phone Number | Valid |
|---|---|
| +12025550123 | |
| +441234567890 | |
| +33123456789 | |
| +861234567890 | |
| +61212345678 | |
| +81312345678 | |
| 15551234567 | |
| +1 | |
| + | |
| 123 | |
| +01234567890 | |
| +123456789012345678 | |
| abc123456789 | |
| +1-202-555-0123 | |
| (202) 555-0123 | |
| 202.555.0123 |
US/National Format
For validating US phone numbers in common national formats with optional parentheses, dashes, dots, or spaces as separators, you can use a more flexible pattern. This validates 10-digit US phone numbers in various formats.
Explanation
^- Start of the string.\(?- Optional opening parenthesis.\d{3}- Exactly three digits (area code).\)?- Optional closing parenthesis.[-.\s]?- Optional separator: dash, dot, or space.\d{3}- Exactly three digits (central office code).[-.\s]?- Optional separator: dash, dot, or space.\d{4}- Exactly four digits (line number).$- End of the string.
Implementation
Test Cases
| Phone Number | Valid |
|---|---|
| 2025550123 | |
| (202)5550123 | |
| (202) 555-0123 | |
| 202-555-0123 | |
| 202.555.0123 | |
| 202 555 0123 | |
| (202)555-0123 | |
| +12025550123 | |
| 123 | |
| 12345 | |
| (202) 555-012 | |
| 202-555-012 | |
| abc-def-ghij | |
| 202-555-01234 |