Developer QA tool

Phone number regex tester

Run a JavaScript regular expression against sample input and inspect every match without sending data to a server.

Test a pattern

A regex is not global phone validation. Numbering plans change and many countries use variable lengths. Prefer a maintained numbering library for production decisions.

Use regex for input shape

A small expression can detect disallowed characters, a missing plus sign or an obvious length boundary. It is useful at the edge of a form when the error message describes only that limited check.

Do not encode a global plan

One expression cannot accurately model every national plan, shared calling code, trunk prefix and future allocation. Treat a match as parseable input, not proof that a destination exists.

Test failure cases

Include empty input, punctuation, extensions, too few and too many digits, unsupported calling codes and pasted text. A good test suite explains why each negative case should fail.

JavaScript regex checklist

CheckWhy it matters
Flagsg, i and u can change matching behavior and repeated execution.
AnchorsUse ^ and $ when the entire field must match, not only one substring.
BacktrackingAvoid nested ambiguous quantifiers that can become slow on long hostile input.
NormalizationDecide whether punctuation is accepted and removed before applying structural checks.

Safe regex boundaries

A broad phone regex is best used as a first-pass syntax filter. It can reject alphabetic text in a telephone field, catch a missing plus sign in an E.164-only API parameter or identify candidate numbers inside a block of support text. It should not decide whether a value can receive SMS, whether the subscriber exists or whether the user controls the destination.

Keep the regular expression small and explain its promise in the error message. If your pattern only permits digits and a leading plus sign, the message should say that. Do not tell the user "invalid phone number" when the real problem is "only digits and + are allowed in this field". Clear wording reduces support tickets and keeps the regex from becoming a fake validator.

Examples of useful negative cases

After the regex check passes, hand the cleaned value to a country-aware parser and record which layer produced each failure.

Review a regex before shipping it

Every production pattern should have a short comment or test name that says what it is allowed to prove. If the pattern is only a text extractor, do not reuse it as a form validator. If it is an E.164 syntax check, do not reuse it for national input. Run long hostile strings through the expression before it touches a public form, and keep a country-aware parser behind it for business decisions.

When a regex changes, rerun both examples that should match and examples that should fail. A looser pattern may appear to reduce support friction while quietly accepting garbage input. A stricter pattern may look cleaner while blocking legitimate pasted values with spaces, non-breaking spaces or extensions that your product actually needs to preserve.

Document the allowed characters, maximum length and whether extraction from free text is supported. Keep those notes beside the tests so future edits do not change the promise of the field silently.

Keep a plain-language note in the product spec explaining whether this regex is a filter, extractor or validator. Future maintainers need that context before they tighten or loosen the expression. If the expression appears in multiple apps, publish one shared test fixture file so the same edge cases protect every implementation.