Agent Wranglers

NTF-26Recommended

Store phone numbers in E.164

Parse every phone number with a phone number library, and store it in E.164 format with its country.

How to do it

E.164 is a plus sign, the country code and the number, with no spaces, such as +46701234567. Parse with a port of Google's libphonenumber, such as libphonenumber-js or phonenumbers for Python, using the visitor's country as the default. Reject numbers that are not valid. Check the line type before sending codes, because landlines cannot receive texts. Use type="tel" and autocomplete="tel" in the form. Parse once, as Parse input once at the boundary says, and accept every country as Accept names and addresses from every country asks.

Examples

SQL example

-- Must return 0 rows
SELECT id FROM users WHERE phone IS NOT NULL AND phone !~ '^\+[1-9][0-9]{6,14}$';

JavaScript example

import { parsePhoneNumberWithError } from 'libphonenumber-js';

export function toE164(input, defaultCountry) {
  const number = parsePhoneNumberWithError(input, defaultCountry);
  if (!number.isValid()) throw new Error('Invalid phone number');
  return { e164: number.number, country: number.country };
}

Python example

import phonenumbers

def to_e164(raw: str, default_country: str) -> dict:
    number = phonenumbers.parse(raw, default_country)
    if not phonenumbers.is_valid_number(number):
        raise ValueError("Invalid phone number")
    return {"e164": phonenumbers.format_number(number, phonenumbers.PhoneNumberFormat.E164),
            "country": phonenumbers.region_code_for_number(number)}

How to check

A database query finds no phone number that fails the pattern ^\+[1-9][0-9]{6,14}$. A unit test turns "070-123 45 67" with country SE into +46701234567.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/notifications/text-messages/e164-phone-numbers">Agent Wranglers notifications playbook</a>. In code you write, add a comment next to it with the same link.