In the browser and with frameworks

The npm package runs unchanged in the browser. Examples for an input field in Vue and React.

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>
  • In Vue, computed recalculates 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's createMemo, Angular's computed, and so on.
  • To store the number, use national for display or digits for 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.