In the browser and with frameworks
The npm package is the same in Node.js and in the browser. The numbering table is bundled as a JavaScript module, so the package reads no files and makes no requests. Import it with Vite, webpack, or any other bundler like any other package. With the table, it is about 14 KB gzipped.
japan-phone is not tied to any framework. normalize and extract are plain functions that take a string and return a result. Call them wherever your framework derives a value from input. No framework-specific package or setup is needed.
In an input field
Call normalize on each change, and show either the formatted number or a prompt to check it.
<script setup lang="ts">
import { computed, ref } from "vue";
import { normalize } from "@kurashina-design/japan-phone";
const input = ref("");
const phone = computed(() => normalize(input.value));
</script>
<template>
<input v-model="input" type="tel" autocomplete="tel">
<p v-if="phone">{{ phone.national }}</p>
<p v-else-if="input">Please check the phone number.</p>
</template>
import { useState } from "react";
import { normalize } from "@kurashina-design/japan-phone";
export function PhoneField() {
const [input, setInput] = useState("");
const phone = normalize(input);
return (
<>
<input type="tel" autoComplete="tel" value={input} onChange={(event) => setInput(event.target.value)} />
{phone ? <p>{phone.national}</p> : input && <p>Please check the phone number.</p>}
</>
);
}
- In Vue,
computedrecalculates only when the input changes. In React, calling it on every render is fine: one check is cheap. - The same call works in Svelte's
$derived, Solid'screateMemo, Angular'scomputed, and so on. - To store the number, use
nationalfor display ordigitsfor digits only. See the examples.
Server-side rendering
The same package runs unchanged in server-side rendering (Nuxt, Next.js, and others). The server and the browser use the same table, so their results match.