Tutorial

How to Validate Phone Numbers in E.164

Validate phone numbers in E.164 format with libphonenumber, so international numbers are normalized and invalid lengths are rejected before SMS send.

On this page

  1. Introduction
  2. Parse with a default region
  3. Check isPossible and isValid
  4. Format to E.164
  5. Reject unsupported regions
  6. Store the canonical form
  7. Test valid and invalid numbers
  8. Common pitfalls
  9. Conclusion

Introduction

The hardest part of validate international phones is not writing the code — it is deciding what the code is allowed to reject. A libphonenumber setup that accepts everything is easy to ship and expensive to operate, because every malformed input becomes a production surprise. The discipline is to enumerate the shapes the system actually receives, encode them in libphonenumber, and let the library reject the rest loudly. This keeps the happy path fast and the failure path visible.

Most teams adopt libphonenumber for validate international phones after an incident teaches them that unvalidated input is load-bearing. The incident is the expensive way to learn the lesson; a quarterly review of the input shapes is the cheap way. Pin the shapes with libphonenumber, refuse the deploy that breaks the pin, and treat the pin as a contract the rest of the system can rely on. A contract that is not enforced is a wish.

The sections below walk through the concrete decisions: where to put the libphonenumber check, how to keep it fast under load, what to do when the contract changes, and how to test the failure paths so they degrade on purpose instead of by accident.

Parse with a default region

Parse with a default region is the first decision because it determines what the rest of the libphonenumber pipeline can assume. A wrong choice here turns into validate international phones under load, and the invariant you thought libphonenumber guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.

The test for Parse with a default region is the one most teams skip: drive libphonenumber to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.

import { parsePhoneNumber } from 'libphonenumber-js';
function toE164(input, region = 'US') {
  const p = parsePhoneNumber(input, region);
  if (!p?.isValid()) throw new Error('invalid phone');
  return p.format('E.164');
}

The javascript above is the shape to ship for validate international phones. It is small enough to audit and specific enough that the failure behavior is obvious. Adapt the constants to your traffic and latency budget, and resist the urge to handle cases the system has not seen yet.

Check isPossible and isValid

The mechanism behind Check isPossible and isValid is straightforward in libphonenumber, but the consequences ripple. A wrong choice here turns into validate international phones under load, and the invariant you thought libphonenumber guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.

The test for Check isPossible and isValid is the one most teams skip: drive libphonenumber to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.

Format to E.164

Format to E.164 is where most libphonenumber implementations leak performance. A wrong choice here turns into validate international phones under load, and the invariant you thought libphonenumber guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.

The test for Format to E.164 is the one most teams skip: drive libphonenumber to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.

test('normalizes to E.164', () => {
  expect(toE164('(415) 555-0132')).toBe('+14155550132');
});

Pin the behavior with this test. A libphonenumber setup that passes this test degrades on purpose instead of by accident, which is the difference between a managed failure and an incident.

Reject unsupported regions

The hard part of Reject unsupported regions is not the happy path — it is what happens when libphonenumber fails halfway. A wrong choice here turns into validate international phones under load, and the invariant you thought libphonenumber guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.

The test for Reject unsupported regions is the one most teams skip: drive libphonenumber to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.

Store the canonical form

Store the canonical form should be observable without a debugger. A wrong choice here turns into validate international phones under load, and the invariant you thought libphonenumber guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.

The test for Store the canonical form is the one most teams skip: drive libphonenumber to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.

Test valid and invalid numbers

Test valid and invalid numbers is a trade, not a winner. A wrong choice here turns into validate international phones under load, and the invariant you thought libphonenumber guaranteed no longer holds. Measure the impact before committing, because the cost of a bad choice is paid in incidents, not in a code review.

The test for Test valid and invalid numbers is the one most teams skip: drive libphonenumber to the failing condition and assert the system degrades gracefully. Write it with a fake clock for time-based logic, a stub for the failure path, and an assertion on the user-visible outcome. A test that passes by timing out is a hope, not a test.

Common pitfalls

The common pitfalls in validate international phones all reduce to one mistake: treating libphonenumber as a formality instead of a contract. The contract is the thing the rest of the system relies on, and a weak contract propagates weakness everywhere.

  • Skipping the libphonenumber boundary so validate international phones enters unvalidated.
  • Assuming libphonenumber is fast without measuring the tail latency under load.
  • Letting validate international phones fail silently instead of failing loudly with a metric.
  • Testing only the happy path so the libphonenumber failure surfaces in production.
  • Sharing mutable state across requests so one bad input poisons the next.

Conclusion

Do not conflate the conclusion with the implementation. They look similar from the outside but the failure modes differ: libphonenumber fails fast and the caller knows, while an unchecked path fails slow and the caller guesses. Choose the one whose failure mode your system can absorb, and document the choice at the call site.

Self-check: write a test that drives libphonenumber to the failing condition and assert the system degrades gracefully. Then validate international phones is a property you maintain, not a feature you hope for.

Sources: libphonenumber-js, E.164

FAQ

Why E.164?
It is the canonical international format. SMS and voice APIs require it.
Should I store the original input?
No. Store the E.164 form and a region code. The original is display-only.

Related Examples