How to validate phone numbers using regex
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Regex can validate the format of a phone number, but only after you decide what format you actually want to allow. A single "universal phone regex" is usually a bad idea because phone numbers vary by country, separators, extensions, and whether the input is meant for storage or for user-friendly display.
The practical strategy is to separate concerns: normalize the input, apply a regex for the format you support, and use a phone-number library when you need real region-aware validation. Regex is good at shape checking, not at telecom correctness.
Define the Validation Target First
Before writing any regex, choose one of these scopes:
- a normalized international format such as E.164-style storage
- a country-specific display format such as US local input
- a permissive user-input format that allows spaces and punctuation
Without that decision, the regex will usually be too strict for some users and too permissive for others.
For normalized international storage, a common E.164-like pattern is:
This is useful for a canonical stored value. It is not a good UX pattern for every text box.
Normalize Before Matching
Users type spaces, dashes, parentheses, and other separators. Normalize first so the regex has less work to do:
This keeps the regex focused on phone structure instead of every formatting variant a user might type.
Validate an E.164-Style Number
Once normalized, an international-style check is simple:
This validates structure only. It does not prove that the number is assigned, active, or even valid for a specific country.
Write Country-Specific Regex Only When You Mean It
If your product is intentionally country-specific, a local format regex can be appropriate. For example, a US-oriented pattern:
This is fine if your requirements are really US-specific. It is not fine if your product has international users and the regex silently rejects valid non-US numbers.
Handle Extensions Deliberately
Extensions complicate the pattern. Decide whether the extension belongs in the same field or a separate one. If you keep it in the same field, the regex needs to say so:
In many systems, storing the extension separately is simpler for search, formatting, and downstream integrations.
Use a Library When Correctness Matters
Regex is useful for quick format checks, but it does not know real numbering plans. If your application needs high-quality international validation, use a library such as libphonenumber bindings after the regex or instead of it.
A sensible server-side flow is:
- normalize the input
- apply a regex if you want a quick format gate
- parse with a region-aware library if the business case requires stronger validation
- store the canonical normalized form
That keeps the code honest about what regex can and cannot do.
Common Pitfalls
The biggest mistake is trying to write one regex that handles every country's real phone-number rules. That is usually brittle and misleading.
Another common issue is validating raw unnormalized input. A perfectly valid number may fail only because it contains harmless punctuation.
People also confuse format validation with real phone validation. A string can match the regex and still be a nonexistent or unusable number.
Finally, do not hide the accepted format from users. Validation works best when the API contract and UI both make the expected format clear.
Summary
- Choose the target format before writing the regex.
- Normalize input before matching whenever users may type separators.
- Use an E.164-style regex for canonical international-style storage.
- Write country-specific regex only when your requirements are explicitly local.
- Use a phone-number library when you need region-aware validation beyond format checking.

