OTP & verification

E.164 Phone Number Formatting and Validation, Explained

What E.164 is, why you should store every phone number in it, how to normalize messy user input, and when to use libphonenumber instead of a regex.

Updated · 3 min read

If you send SMS or WhatsApp messages, every phone number in your database should be in one format: E.164. It is the international standard used by networks and messaging APIs, and storing numbers any other way leads to failed messages, duplicate accounts, and broken lookups.

What E.164 looks like

An E.164 number has:

  • a + sign,
  • the country code (1 to 3 digits),
  • the national number without its leading trunk 0,
  • no spaces, dashes, or brackets,
  • at most 15 digits in total.
CountryLocal formatE.164
Egypt010 1234 5678+201012345678
Saudi Arabia050 123 4567+966501234567
UAE050 123 4567+971501234567
United Kingdom07400 123456+447400123456
Germany0151 23456789+4915123456789
United States(201) 555-0123+12015550123

The basic shape check

A regex can check the general shape of an E.164 number:

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

This accepts anything that looks like an international number. It does not check whether the number is valid for its country, or whether it is a mobile number.

Normalizing user input

Users type numbers in many ways. Normalize before validating:

  1. Convert non-Latin digits (for example Arabic-Indic ٠–٩) to 0–9.
  2. Remove spaces, dashes, dots, and brackets.
  3. If the input starts with 00, replace it with +.
  4. If it starts with +, treat it as international.
  5. Otherwise combine it with the selected or default country code, removing the national trunk prefix (usually a leading 0).
  6. Validate the result.

Note that a few countries do not drop the leading zero in international format, Italy being the best-known example. That is one reason to use a library for anything beyond a few countries.

Use libphonenumber for real validation

Google's libphonenumber (with ports such as libphonenumber-js) knows the numbering plan for every country. It can:

  • parse a number with a default country,
  • tell you whether it is valid and whether it is mobile or fixed-line,
  • format it for display in local or international style.
import { parsePhoneNumberFromString } from 'libphonenumber-js'

const phone = parsePhoneNumberFromString('010 1234 5678', 'EG')
phone?.isValid()     // true
phone?.getType()     // 'MOBILE' (with the full metadata build)
phone?.number        // '+201012345678'

If you only serve a few countries, a hand-written rule per country can work; see the Egyptian phone number format guide for an example.

Design the input field well

  • Provide a country selector defaulting to the user's likely country.
  • If the user pastes a full +44… number, switch the country automatically.
  • Strip the leading 0 silently instead of showing an error.
  • Use type="tel" and autocomplete="tel" so browsers can autofill.
  • After normalizing, show the formatted number back to the user before sending a code.

Store E.164, display nicely

Store only E.164. Format for display at the edge of your app. Use the stored value for lookups, deduplication, and messaging APIs.

For the next step after validation, see what an SMS OTP is and how it works and OTP best practices.