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-5678is rejected instead of guessed. The form can then ask the user to check it. 03-1234-5678is rejected too. The MIC tables have no03-1234block.- Save
national(ordigits) 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}`);
}
from japan_phone import normalize
# Values typed into a phone number field.
for value in ["03-5253-8111", "03(5253)8111", "090--1234-5678", "03-1234-5678"]:
number = normalize(value)
print(f"ok: {number.national}" if number else f"error: {value}")
import jp.kurashina.japanphone.JapanPhoneNumber;
import jp.kurashina.japanphone.JapanPhone;
import java.util.Optional;
// Values typed into a phone number field.
for (String input : new String[] {"03-5253-8111", "03(5253)8111", "090--1234-5678", "03-1234-5678"}) {
Optional<JapanPhoneNumber> number = JapanPhone.normalize(input);
System.out.println(number.isPresent() ? "ok: " + number.get().getNational() : "error: " + input);
}
Output:
ok: 03-5253-8111
ok: 03-5253-8111
error: 090--1234-5678
error: 03-1234-5678