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}$
| Input | Matches |
|---|---|
(415) 555-2671 | Yes |
415-555-2671 | Yes |
415.555.2671 | Yes |
+1 415 555 2671 | Yes |
4155552671 | Yes |
555-2671 | No, there's no area code |
+44 20 7183 8750 | No, 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"); // nullWhen 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.