research/normalizing-phone-numbers-without-a-library
Normalizing Phone Numbers Typed Into a Form Without a Library: What Simple Rules Get Wrong
Engineering Note · December 2, 2026 · 10 minute read · Node.js · Forms · Data · Validation
The usual way to clean a phone number typed into a form, keep the digits, drop a leading 1 and require ten, handles every ordinary US format. We tested it in Node.js against 25 awkward inputs and found six kinds of mistakes: it rejected numbers with extensions, rejected every international number, accepted 000-000-0000 and 123-456-7890, read the 011 international prefix as an area code, deleted full-width and Arabic-Indic digits, and turned a German number into a US one. A longer rule set, still without a library, fixed all of those in our test list, and whenever it accepted a number it produced the same E.164 string (+15025550142) as libphonenumber-js. What no simple rule can do is tell a real area code from an unused one: on 100,000 random numbers, ours accepted about 26,000 that the library rejected.
Everything below was run on Linux with Node.js 22.23.5 (Unicode 17.0 data), and libphonenumber-js 1.13.17 from npm as the reference, using its default metadata unless we say otherwise. The outputs are the real ones from these runs. This note goes with today’s article, Your Customer List Is an Asset, and with the other note, Deduplicating Customer Records From Two CSV Exports, where phone matching only works once every number is in one format.
What does the simple rule look like?
// Rule 1: keep the digits, drop a leading 1, require ten.
export function naive(input) {
let d = input.replace(/\D/g, "");
if (d.length === 11 && d.startsWith("1")) d = d.slice(1);
return d.length === 10 ? "+1" + d : null;
}
The reference is libphonenumber-js, which its package describes as a simpler and smaller rewrite of Google’s libphonenumber library in JavaScript. We parsed every input with parsePhoneNumberFromString(input, "US"), so a number without a country code is read as a US number, and recorded the E.164 number, any extension and isValid().
Which inputs did we try?
Twenty-five strings: nine ordinary US formats, three with extras (an extension or a “(cell)” label), seven that are wrong or ambiguous, four international numbers, and two written with non-ASCII digits. One of the ordinary formats uses non-breaking spaces between the groups, which happens when a number is copied from a web page.
What did the simple rule get wrong?
The nine ordinary formats all came out as +15025550142 from both the simple rule and the library: (502) 555-0142, 502.555.0142, 502 555 0142, +1 502-555-0142, 1-502-555-0142, 1 (502) 555-0142, +15025550142, tel:+1-502-555-0142, and the version with non-breaking spaces. “502 555 0142 (cell)” also worked, because the label has no digits.
The rest, with the output of the simple rule, our second rule set (below) and the library:
| Input | Simple rule | Rule 2 | libphonenumber-js |
|---|---|---|---|
502-555-0142 ext. 12 | null | +15025550142 x12 | +15025550142 x12 valid |
502-555-0142 x12 | null | +15025550142 x12 | +15025550142 x12 valid |
555-0142 | null | reject: 7 digits | +15550142 invalid |
502-555-014 | null | reject: 9 digits | +1502555014 invalid |
5025550142 5025550199 | null | reject: 20 digits | no parse |
1-800-FLOWERS | null | reject: letters in the number | +11800 invalid |
+44 20 7946 0958 | null | +442079460958 (intl) | +442079460958 valid |
011 44 20 7946 0958 | null | +442079460958 (intl) | +442079460958 valid |
+52 55 1234 5678 | null | +525512345678 (intl) | +525512345678 valid |
(011) 555-0142 | +10115550142 | reject: international, wrong length | +5550142 invalid |
+49 30 123456 | +14930123456 | +4930123456 (intl) | +4930123456 valid |
123-456-7890 | +11234567890 | reject: area code or exchange starts with 0 or 1 | +11234567890 invalid |
000-000-0000 | +10000000000 | reject: area code or exchange starts with 0 or 1 | +10000000000 invalid |
502-555-0142 (full-width) | null | +15025550142 | +15025550142 valid |
٥٠٢٥٥٥٠١٤٢ (Arabic-Indic) | null | +15025550142 | +15025550142 valid |
Grouped by what went wrong:
- Extensions were rejected.
ext. 12adds two digits, so the count is twelve. - Every international number was rejected, and one was quietly converted.
+49 30 123456has exactly ten digits once the+is gone, so the simple rule turned a number the library places in Germany (country code 49) into+14930123456, a US-style number in area code 493. Nothing flags it, and the wrong number is stored. - Placeholders passed.
000-000-0000and123-456-7890have ten digits, so they were accepted. The library marked both invalid. 011was misread. libphonenumber-js reads a leading011as the international dialing prefix: it parsed011 44 20 7946 0958as a UK number,+442079460958. The simple rule treated(011) 555-0142as a US number with area code 011.- Non-ASCII digits were deleted. In JavaScript,
\dand\Dmatch only ASCII0to9. Full-width digits and Arabic-Indic digits are “non-digits”, soreplace(/\D/g, "")removed every one of them and left an empty string.
What does a better rule set look like without a library?
// Rule 2: a few more rules, still no library.
// Returns { e164, ext } for a US/Canada-style number, { intl } for anything
// that starts with + or 011 and is not +1, or { error } with a reason.
export function better(input) {
let s = input.normalize("NFKC").trim().replace(/^tel:/i, "");
// Other decimal digits (Arabic-Indic, Devanagari...): map each to 0-9.
s = s.replace(/\p{Nd}/gu, (c) => {
if (c >= "0" && c <= "9") return c;
const cp = c.codePointAt(0);
let start = cp;
while (/\p{Nd}/u.test(String.fromCodePoint(start - 1))) start--;
return String((cp - start) % 10);
});
let ext = null;
const m = s.match(/\s*(?:ext\.?|extension|x|#)\s*(\d{1,6})\s*$/i);
if (m) { ext = m[1]; s = s.slice(0, m.index); }
if (/[a-z]{2,}/i.test(s.replace(/\(\s*(cell|mobile|home|work)\s*\)/i, "")))
return { error: "letters in the number" };
let d = s.replace(/\D/g, "");
const plus = s.startsWith("+");
if ((plus && !d.startsWith("1")) || d.startsWith("011")) {
if (d.startsWith("011")) d = d.slice(3);
return d.length >= 8 && d.length <= 15 ? { intl: "+" + d, ext } : { error: "international, wrong length" };
}
if (d.length === 11 && d.startsWith("1")) d = d.slice(1);
if (d.length !== 10) return { error: `${d.length} digits` };
if (!/^[2-9]\d{2}[2-9]/.test(d)) return { error: "area code or exchange starts with 0 or 1" };
return { e164: "+1" + d, ext };
}
What each step is for:
normalize("NFKC")turns full-width digits, and other “compatibility” forms, into ordinary ASCII.- The
\p{Nd}mapping handles decimal digits NFKC leaves alone, such as Arabic-Indic. Unicode stores each script’s digits in a run of consecutive code points, so a digit’s value is its distance from the start of its run. - The extension is split off before digits are taken, and kept.
- Letters reject vanity numbers like
1-800-FLOWERS, except a label in parentheses. - A leading
+other than+1, or011, marks an international number. We only check its length (8 to 15 digits) and store it as typed. - The 2-to-9 check on the first digit of the area code and of the exchange (the fourth digit) is the rule we added after seeing which inputs the library rejected. It catches
000,123and011.
Rule 2 got all 25 inputs into the same normalized form as the library, or rejected them where the library said invalid.
How did we find a bug in the digit mapping?
Our first version looked back at most nine code points to find the start of a digit run. To check it, we ran every one of the 1,114,112 Unicode code points through \p{Nd} (770 matched) and compared our mapping with a full scan to the start of each run. Nine characters disagreed, U+116DA to U+116E2, which sit in a run of twenty consecutive digits that begins at U+116D0. A twenty-digit run holds two sets of 0 to 9, so the value is the distance from the start modulo 10. The version above scans to the start and uses % 10. After the fix, our mapping agreed with NFKC on every digit NFKC converts.
How close is rule 2 to the library on random numbers?
We generated 100,000 random ten-digit strings, formatted each in one of six common shapes ((502) 555-0142, dots, no separators, +1 with spaces, 1- with dashes, and spaces around dashes), and ran both:
{
N: 100000,
bothAccept: 37965,
onlyMine: 26124,
onlyRef: 603,
bothReject: 35308,
sameNumberWhenMineAccepts: 64089
}
distinct area codes only mine accepted: 332 e.g. 200 211 221 222 230 232 233 237 238 241 243 244
When rule 2 accepted a number, it produced exactly the same E.164 string as the library all 64,089 times. The formatting was never the problem.
The disagreement is about which numbers could exist:
- 26,124 accepted only by rule 2, spread over 332 area codes such as 200 and 211. Our rule checks the shape; the library’s metadata knows which area codes are actually in use. A simple rule cannot know that.
- 603 accepted only by the library. All 603 were numbers it assigned to 21 places other than the US that share the
+1code, such as the Dominican Republic and Jamaica, with an exchange starting with 0 or 1. With libphonenumber-js’s largermaxmetadata, only 58 numbers were accepted by the library alone, and the “only mine” count rose to 27,292. We could not open the numbering plan documentation from this environment to say which answer is right for those numbers.
Neither the rule nor the library can tell whether a number is connected. The library marked (502) 555-0142, (502) 555-5555 and (212) 411-0000 as valid, and rejected (999) 555-0142. A valid shape is not a working phone.
What should a form do with phone numbers?
- Keep three fields: the E.164 number, the extension, and the text the person typed.
- Never convert a number that starts with
+and another country code into a US number. - Reject placeholders and impossible area codes, and tell the person why, in plain words, next to the field.
- If you take numbers from more than one country, use a library. If you need to know a number works, send a text or call.
- Run the same cleanup on the server as in the browser, so a script or an old form cannot skip it. Our note on one config for client and server intake fields covers keeping the two in step.
Limitations
- We tested with libphonenumber-js 1.13.17 as the reference. Its metadata changes as numbering plans change, and its default and
maxmetadata gave different answers for some+1numbers outside the US. - We could not open the North American Numbering Plan’s or the ITU’s documentation from this environment, so the 2-to-9 rule and the 8-to-15 digit range for international numbers are what fit our tests and the library’s results, not quoted rules.
- International numbers were only length-checked. A wrong-length or mistyped international number with a plausible length passes rule 2.
- The extension pattern assumes the extension is at the end and introduced by “ext”, “extension”, “x” or ”#”. Other wording is not recognized.
- The digit mapping and NFKC behavior were tested against the Unicode 17.0 data in Node.js 22.23.5. Other runtimes with older Unicode data may not know some digit runs.
- We did not test what mobile keyboards actually insert into an
<input type="tel">field; the full-width and Arabic-Indic inputs were typed by us.
Related reading
- Deduplicating Customer Records From Two CSV Exports: Matching on Email and Phone, and Where It Merges the Wrong People
- Your Customer List Is an Asset: Where It Lives, Who Can Export It, and How to Keep One Clean Copy
- One Config for Client and Server: A Single Source of Truth for Intake Forms
Frequently asked questions
How do I normalize a US phone number in JavaScript without a library?
Normalize the text with NFKC, remove an extension at the end, keep the digits, drop a leading 1 from an 11-digit number, and require exactly ten digits whose first and fourth digits are 2 to 9. Store the result as +1 followed by the ten digits. In our tests this matched libphonenumber-js's E.164 output for every number it accepted.
Why does replace(/\D/g, '') lose digits?
In JavaScript, \d and \D only cover the ASCII digits 0 to 9, even with the u flag; we checked this in Node.js 22. Full-width digits and Arabic-Indic digits count as non-digits and are deleted, so a valid number becomes an empty string. Normalizing with NFKC fixes full-width digits; other scripts' digits need to be mapped separately.
Can a simple rule tell if a phone number is real?
No. A rule can check the shape of a number, but not whether an area code is in use or the line is connected. In 100,000 random ten-digit numbers, our rule accepted 26,124 that libphonenumber-js rejected, across 332 area codes. Even the library accepted 555 numbers, so the only real check is calling or texting the number.
How should I store phone numbers from a form?
Store the normalized number in E.164 form, a plus sign, the country code and the number with no spaces, such as +15025550142, in one field, the extension in another, and the text the person typed in a third. The original text lets you fix a bad normalization later without asking the customer again.
When should I use libphonenumber-js instead of my own rules?
When you take numbers from more than one country, or need to know whether a number could be valid rather than whether it is shaped right. Our own rules were fine for cleaning US and Canada numbers into one format, but they only checked the length of international numbers.