Phone Number Regex (E.164)
Check that a phone number is in E.164 form: a plus sign, a country code that never starts with 0, and no more than 15 digits in all.
One case per line; the m flag makes ^ and the end anchor work line by line.
Link optionspattern only
The address bar holds the pattern, flags and replacement, so Copy link shares them. Your test text stays out of it unless you include it (up to 2,000 characters), because it may be private. Nothing is sent to a server.
Matches
6matches
The first is “+14155550123”, at position 0.
- Groups
- none
- Characters matched
- 78 of 176
What the pattern meansHover or tap a part to see it in the pattern and what it matched
How this works: Method, 6 sources, Checked against 1 worked example,
How this works
Method
Your pattern runs in your own browser’s JavaScript engine, in a background worker that is stopped after 1.5 seconds, so a pattern that backtracks catastrophically can’t freeze the page. The explanation comes from our own parser of the ECMAScript pattern grammar, checked against the engine; what each part matched is found by wrapping that part in one more group and running the pattern again. Conversions to other flavours only rewrite the syntax and list what their documentation says works differently.
Sources
How it’s tested
One worked example for this page is checked by automated tests before every release: given the inputs, the tool must show the expected answer.
Changes
- First published, with a library of 19 common patterns and their test cases.
Worked example
Take +14155550123, the first case in the tester. Reading the pattern left to right, each part takes its share of the text:
^The start of the text (matches a position, no characters)\+“+” matches +[1-9]One character: 1–9 matches 1\d{1,14}One to 14 digits (0–9) matches 4155550123$The end of the text (matches a position, no characters)
How the E.164 phone regex works
E.164 is the numbering plan the ITU publishes for every public telephone network, and its written form is the most useful thing to store: a plus sign, the country code and the national number, with no spaces, brackets or trunk prefix. The pattern checks exactly that shape. The caret pins the match to the start, and the escaped plus sign \\+\ asks for a literal plus (an unescaped \+\ would mean "one or more").
Next comes \[1-9]\, the first digit of the country code. Country codes run from 1 (North America) to three digits such as 353 (Ireland), and none of them begins with 0, so a leading zero is a sure sign that someone typed a national number with a + in front of it. Then \\d{1,14}\ allows between one and fourteen more digits, which keeps the total at 15 or fewer, the ceiling E.164 sets. The dollar sign at the end means nothing may follow.
People rarely type numbers this way, so test after cleaning up, not before. In JavaScript, \input.replace(/[\s().-]/g, '')\ removes spaces, brackets, dots and hyphens, turning "+1 (415) 555-0123" into "+14155550123". If the visitor typed a national number without a country code, you have to add one from context (their country setting, say) before this pattern can pass it.
- E.164 numbers contain only digits and are at most fifteen digits long; the country code is one to three digits. Source: E.164 (Wikipedia summary of ITU-T Recommendation E.164).
- North American Numbering Plan numbers are ten digits, written NXX NXX-XXXX, where N is a digit from 2 to 9, under the shared country code 1. Source: North American Numbering Plan (Wikipedia).
- The ITU publishes E.164, “The international public telecommunication numbering plan”, as an ITU-T Recommendation. Source: ITU-T Recommendation E.164.
Test cases
Every case runs as an automated test of this page’s pattern, so the table can’t drift from what the pattern really does.
| Text | Result | Why |
|---|---|---|
| +14155550123 | Passes | a US number: country code 1, then ten digits |
| +442079460958 | Passes | a London number, with the national 0 dropped |
| +61491570156 | Passes | an Australian mobile, country code 61 |
| +819012345678 | Passes | a Japanese mobile, twelve digits in all |
| +33612345678 | Passes | a French mobile, country code 33 |
| +123456789012345 | Passes | fifteen digits, the most E.164 allows |
| 14155550123 | Fails | no plus sign in front |
| +1 415 555 0123 | Fails | spaces: strip them before testing |
| (415) 555-0123 | Fails | a US national format, not international |
| +0441234567 | Fails | no country code starts with 0 |
| +1234567890123456 | Fails | sixteen digits, one too many |
| +44 (0)20 7946 0958 | Fails | the bracketed trunk 0 is for local dialling only |
What it doesn’t check
- The pattern checks the shape of a number, not whether it exists, is assigned, or can receive texts: only sending a code proves that.
- It does not know each country’s real lengths, so +1 followed by three digits passes even though North American numbers always have ten digits after the 1.
- For a US or Canadian number typed nationally, a separate pattern such as \
\(?[2-9]\d{2}\)?[-. ]?[2-9]\d{2}[-. ]?\d{4}\(anchored at both ends) follows the NANP rule that area codes and exchange codes start with 2 to 9. - Extensions (“ext. 204”) and pauses are not part of E.164; store them in a separate field.
- For full per-country validation, use a maintained library built on the numbering data each country publishes rather than a growing regex.
The same pattern in other languages
Converted automatically from the JavaScript version; the tester above always runs JavaScript.
| Flavour | Pattern | Notes |
|---|---|---|
| Python | (?a)^\+[1-9]\d{1,14}\Z | Python's \d also matches digits from other scripts (such as Arabic-Indic digits) in str patterns; use [0-9] or the re.ASCII flag to keep it to 0–9. |
| PCRE | ^\+[1-9]\d{1,14}\z | |
| Go | ^\+[1-9]\d{1,14}$ | |
| Java | ^\+[1-9]\d{1,14}\z | |
| .NET | ^\+[1-9]\d{1,14}\z | In .NET, \d and \w match any Unicode digit or letter. Pass RegexOptions.ECMAScript, or write [0-9] and [A-Za-z0-9_], for JavaScript’s ASCII-only meaning..NET's \d matches any Unicode decimal digit unless you pass RegexOptions.ECMAScript; [0-9] is the safe choice. |
Sources
Frequently Asked Questions
What is the best regex for phone numbers?
For storage and international use, check the E.164 form: a plus sign, a first digit from 1 to 9, then up to 14 more digits. Clean up spaces, brackets and hyphens first. No single regex can check every country’s rules, so use a phone-number library when exact validity matters.
Why does the pattern reject a number with spaces?
E.164 is written without separators, so the pattern asks for digits only. Remove spaces, dots, hyphens and brackets from the input before testing it, and show the number back to the visitor in a friendly grouped format if you like.
How long can an international phone number be?
Fifteen digits at most, counting the country code but not the plus sign. Many numbers are shorter: a US number is 11 digits with its country code, a UK London number 12.
Can a regex tell me if a phone number is real?
No. It only checks the format. Whether a number is assigned, active or able to receive an SMS can only be found by a carrier lookup or by sending a verification code.