Regex tester

Regex for phone numbers

Phone numbers come in many formats, so pick the pattern that matches what you store. For international numbers, the standard storage format is E.164: a +, a country code and up to 15 digits in total, with no spaces.

^\+[1-9]\d{1,14}$

The tester below checks some samples against it.

Regex tester
Test textTEXT

Press Command or Control plus Enter to run. Tab inserts indentation; press Escape, then Tab, to leave the editor.

Result
Your result will appear here.
Ready6 lines · 76 chars

US numbers as people type them

This accepts 10-digit US numbers with an optional +1, and spaces, dots, dashes or parentheses between the groups:

^(?:\+1[-. ]?)?\(?\d{3}\)?[-. ]?\d{3}[-. ]?\d{4}$

InputMatches
(415) 555-2671Yes
415-555-2671Yes
415.555.2671Yes
+1 415 555 2671Yes
4155552671Yes
555-2671No, there's no area code
+44 20 7183 8750No, it isn't a US number

Normalize before you validate

Let people type numbers however they like, strip the formatting, and then check the digits:

function toE164US(input) {
  const digits = input.replace(/\D/g, "");
  const national = digits.length === 11 && digits.startsWith("1") ? digits.slice(1) : digits;
  return /^\d{10}$/.test(national) ? `+1${national}` : null;
}

toE164US("(415) 555-2671");  // "+14155552671"
toE164US("555-2671");        // null

When a regex isn't enough

A regex checks the shape of a number, not whether it can exist. Number lengths and valid prefixes differ for every country. If you accept international numbers, use Google's libphonenumber (libphonenumber-js in JavaScript), which knows the rules for each country, and store the result in E.164.

Frequently asked questions

What is E.164?

The international standard for phone numbers: +, the country code and the national number, at most 15 digits, with no spaces or punctuation. SMS providers such as Twilio expect numbers in this format.

Why does +0123456 fail?

Country codes never start with 0, so the pattern requires the first digit after + to be 1 to 9.

Should I store phone numbers as numbers?

No. Store them as strings. A number type drops the leading + and zeros, and phone numbers are never used for arithmetic.

More regex tester guides

Open the full regex tester →

From the blog