E.164 電話號碼格式錯誤如何導致短訊及通話失敗
Phone numbers in E.164: the formatting bugs that break SMS and calls
Packet Exchange 的文章建議將電話號碼統一儲存為 E.164 格式,輸入時正規化、僅於顯示時轉換,以避免格式錯誤令短訊及通話失敗。常見問題包括英國號碼保留國內撥號前綴 0、重複加入國碼、CSV 匯出丟失加號或轉成科學記號,以及同一號碼因格式不同而重複建檔。
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
-
The kept trunk zero.
+4407700900123looks 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. -
Double country codes. A form pre-fills
+44, the user types447700900123, and you store+44447700900123. -
Spreadsheet damage. A CSV export turns
447700900123into4.47701E+11, or strips the leading plus. -
Duplicate users.
07700900123and+447700900123are the same person, but your unique index does not know that. One-time codes go to the wrong account record. - 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:
- Runnable examples in eight languages, including number lookup, SMS and verification
- Node.js and TypeScript library and Python library
-
MCP server: point Claude, Cursor or any MCP client at
https://packetexchange.io/mcp/httpand your agent can look up and price a number before it sends. Read the MCP overview
What is the strangest phone number format you have found in a production database?
來源:DEV Community · dev.to