Start with the validation claim
A pattern such as ^\+?[1-9]\d{7,14}$ can enforce a plus sign, a non-zero first digit and a broad digit length. It does not establish that the calling code is assigned, that the national prefix is possible or that the destination is reachable. Name the check accurately: syntax, not global validity.
Use the phone regex tester to inspect matches and invalid-pattern errors in the browser.
Prefer country-scoped patterns
If a product supports a small fixed set of countries, a country-scoped expression can provide useful immediate feedback. Keep the country outside the raw number field, normalize presentation characters first and test the remaining national digits against that country's current metadata.
Do not silently infer a country from the first few national digits. Shared calling codes, overlapping prefixes and number portability make that shortcut unreliable.
Avoid common regex traps
- Forgetting anchors and accepting a valid-looking substring inside invalid text.
- Using
\dwithout deciding whether non-ASCII decimal digits are acceptable. - Allowing repeated plus signs or punctuation in arbitrary positions.
- Embedding a stale list of country calling codes in application code.
- Rejecting extensions when the product actually needs to store them separately.
Test behavior, not only examples
Include boundaries one digit below and above the permitted length, pasted spaces, full-width characters, newlines, extensions and multiple values in one string. Reset a global JavaScript regex between repeated tests when stateful methods can change lastIndex.
For production parsing, use maintained numbering metadata such as libphonenumber. The language behavior of JavaScript patterns is defined by the ECMAScript RegExp specification.