Forms

Validate a phone number field

normalize
Accept full-width input, reject numbers that do not exist, and save one notation.
Try it
Try 090--1234-5678 or 03-1234-5678.

Saved as 03-5253-8111

How it works

normalize returns the number with its hyphens, or an empty result when the input does not fit the numbering plan and the MIC tables.

  • Full-width digits and brackets are accepted, so users can type in either mode of their keyboard.
  • Broken input such as 090--1234-5678 is rejected instead of guessed. The form can then ask the user to check it.
  • 03-1234-5678 is rejected too. The MIC tables have no 03-1234 block.
  • Save national (or digits) so that every record uses the same notation.

In each language

import { normalize } from "@kurashina-design/japan-phone";

// Values typed into a phone number field.
for (const input of ["03-5253-8111", "03(5253)8111", "090--1234-5678", "03-1234-5678"]) {
  const number = normalize(input);
  console.log(number ? `ok: ${number.national}` : `error: ${input}`);
}

Output:

ok: 03-5253-8111
ok: 03-5253-8111
error: 090--1234-5678
error: 03-1234-5678