Skip to content

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.

JavaScript
176 characters

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.

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.

How we make toolsReport a mistake

Worked example

Take +14155550123, the first case in the tester. Reading the pattern left to right, each part takes its share of the text:

  1. ^The start of the text (matches a position, no characters)
  2. \+“+” matches +
  3. [1-9]One character: 1–9 matches 1
  4. \d{1,14}One to 14 digits (0–9) matches 4155550123
  5. $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.

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.

Test cases for the Phone number regex
TextResultWhy
+14155550123Passesa US number: country code 1, then ten digits
+442079460958Passesa London number, with the national 0 dropped
+61491570156Passesan Australian mobile, country code 61
+819012345678Passesa Japanese mobile, twelve digits in all
+33612345678Passesa French mobile, country code 33
+123456789012345Passesfifteen digits, the most E.164 allows
14155550123Failsno plus sign in front
+1 415 555 0123Failsspaces: strip them before testing
(415) 555-0123Failsa US national format, not international
+0441234567Failsno country code starts with 0
+1234567890123456Failssixteen digits, one too many
+44 (0)20 7946 0958Failsthe 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.

The pattern in Python, PCRE, Go, Java and .NET
FlavourPatternNotes
Python(?a)^\+[1-9]\d{1,14}\ZPython'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}\zIn .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.