Regex Pattern for Phone Numbers
Phone numbers appear in dozens of formats: with country codes, spaces, dashes, dots, or parentheses around area codes. This example pattern handles common North American formats and can be extended for international numbers. Use the tester to verify your pattern accepts the formats your users enter while rejecting clearly invalid strings. A permissive pattern followed by normalization is usually better than a strict pattern that rejects valid numbers.
Example
/^[+]?[(]?[0-9]{3}[)]?[-s.]?[0-9]{3}[-s.]?[0-9]{4,6}$/
# Test cases:
+1 (555) 123-4567
555-123-4567
555.123.4567
5551234567
(555)123-4567
123
not-a-phoneFAQ
- Should I normalize phone numbers before storing them?
- Yes. Strip all non-digit characters and store in E.164 format (+15551234567). This makes comparison, formatting, and SMS delivery much easier.
- How do I handle international phone numbers?
- International formats vary widely by country. Consider using a library like libphonenumber instead of regex for production international phone validation.
- Can I match extension numbers?
- Add an optional group like (\s?(ext|x)\s?\d{1,4})? at the end of your pattern to capture common extension formats.
Related Examples
Regex Pattern for Email Validation
Email validation regex is one of the most commonly written and miswritten patter...
Regex Pattern for Date FormatsDate strings come in many formats and regex can catch obvious formatting errors ...
Regex Pattern for Password StrengthPassword strength requirements are commonly enforced with a series of lookahead ...