跳到正文
DEV Community· Packet Exchange·· 54 分鐘前AI 評分50

E.164 電話號碼格式錯誤如何導致短訊及通話失敗

Phone numbers in E.164: the formatting bugs that break SMS and calls

AI 導讀

Packet Exchange 的文章建議將電話號碼統一儲存為 E.164 格式,輸入時正規化、僅於顯示時轉換,以避免格式錯誤令短訊及通話失敗。常見問題包括英國號碼保留國內撥號前綴 0、重複加入國碼、CSV 匯出丟失加號或轉成科學記號,以及同一號碼因格式不同而重複建檔。

正文

Cover image for Phone numbers in E.164: the formatting bugs that break SMS and calls

Packet Exchange profile image Packet Exchange

AI disclosure: this article was written with the help of AI and checked by a human before publishing.

Most "SMS not delivered" and "call failed" tickets I have seen were not network problems. They were phone numbers stored in a shape the carrier could not route. The fix is boring and it works: store every number in E.164 and convert at the edges.

What E.164 is

E.164 is the international numbering format. It is a plus sign, the country code, then the national number with no trunk prefix, no spaces and no punctuation. At most 15 digits after the plus.

  • UK mobile written locally: 07700 900123
  • The same number in E.164: +447700900123
  • A Spanish mobile: +34612345678
  • A US number: +12025550143

Note what happened to the UK number. The leading 0 is a trunk prefix used for dialling inside the country. It is dropped when the country code goes on. Leaving it in (+4407700900123) is the single most common bug I see.

Five bugs that come from not normalising

  1. The kept trunk zero. +4407700900123 looks plausible and fails. A blanket "strip the zero" rule is not the answer either: Italian landlines keep their leading zero after +39, so the same rule breaks them.
  2. Double country codes. A form pre-fills +44, the user types 447700900123, and you store +44447700900123.
  3. Spreadsheet damage. A CSV export turns 447700900123 into 4.47701E+11, or strips the leading plus.
  4. Duplicate users. 07700900123 and +447700900123 are the same person, but your unique index does not know that. One-time codes go to the wrong account record.
  5. Formatting characters. Spaces, dashes, brackets and the invisible characters that come with copy and paste from a web page.

Normalise on the way in

Do not write your own parser. Use libphonenumber or one of its ports, and pass the user's country as the default region.

import { parsePhoneNumberFromString } from 'libphonenumber-js';

export function toE164(raw, defaultCountry = 'GB') {
  const n = parsePhoneNumberFromString(String(raw).trim(), defaultCountry);
  if (!n || !n.isValid()) return null; // reject, do not guess
  return n.number; // always E.164, for example +447700900123
}
import phonenumbers

def to_e164(raw: str, default_region: str = "GB") -> str | None:
    try:
        n = phonenumbers.parse(raw, default_region)
    except phonenumbers.NumberParseException:
        return None
    if not phonenumbers.is_valid_number(n):
        return None
    return phonenumbers.format_number(n, phonenumbers.PhoneNumberFormat.E164)

Rules that keep it fixed

  • Store E.164 only. Put a check constraint on the column that it starts with + and has 8 to 15 digits.
  • Make the unique index on the E.164 value, not on whatever the user typed.
  • Format for humans only at display time.
  • Send E.164 to every SMS and voice API, even when the docs say local formats are accepted.
  • Treat CSV imports as untrusted input and run every row through the same function.
  • Log the raw input next to the normalised value for a week after launch. It shows you which formats your users really type.

Try it yourself

To check your normaliser against real numbers, sign up and run the number lookup example on its output. A lookup returns the number type, the network and any risk flags, and it costs nothing.

The code is open on GitHub:

What is the strangest phone number format you have found in a production database?

來源:DEV Community · dev.to