Vue Phone Number Input
Vue-phone-number-input is a Vue 2 component designed to provide a comprehensive international phone number input field. It leverages the robust `libphonenumber-js` library for accurate parsing, formatting, and validation of phone numbers. While the current stable version is 1.12.13, the package is explicitly marked as unmaintained by its author. It offers features such as country code selection, automatic number formatting while typing, validator state indication, locale detection, and accessibility improvements. However, for ongoing support, compatibility with newer Vue versions, and continued development, developers are strongly advised to migrate to the `MazPhoneNumberInput` component, which is available within the `maz-ui` library (v2.x for Vue 2 projects and v3.x for Vue 3 projects), as this standalone package receives no further updates.
Common errors
-
[Vue warn]: Failed to resolve component: vue-phone-number-input
cause Attempting to use the Vue 2 component in a Vue 3 application.fixThis component is incompatible with Vue 3. Migrate to `MazPhoneNumberInput` from `maz-ui` v3.x. -
TypeError: Cannot read properties of undefined (reading 'install')
cause Incorrect global component registration or `Vue` is not globally available in a CommonJS or non-browser environment.fixEnsure `Vue` is imported and globally available (e.g., `import Vue from 'vue';`) and use `Vue.component('VuePhoneNumberInput', VuePhoneNumberInput);` without `.default` for ESM imports. If using UMD, refer to the documentation for correct `window.VuePhoneNumberInput.default` usage. -
[Vue warn]: Invalid prop: type check failed for prop "modelValue". Expected String, got Object.
cause `v-model` is bound to an object instead of a string. `vue-phone-number-input` expects a string for the phone number.fixEnsure `v-model` is bound to a string variable (e.g., `v-model="userPhoneNumber"`). The component emits a detailed object on the `@update` event, which should be handled separately if you need the full data. -
Error: Could not find country calling code for country 'XX'
cause An invalid or unsupported ISO 3166-1 alpha-2 country code was provided to a prop like `default-country-code` or implicitly via an initial `v-model` value.fixVerify that the country code (e.g., 'US', 'FR', 'DE') is a valid ISO 3166-1 alpha-2 code and is recognized by `libphonenumber-js`. Refer to a list of standard country codes.
Warnings
- breaking This package is explicitly marked as unmaintained by its author. No further updates, bug fixes, or security patches will be released for `vue-phone-number-input` (v1.x).
- breaking This component is built exclusively for Vue 2. Attempting to use `vue-phone-number-input` directly in a Vue 3 application will lead to component resolution failures and runtime errors.
- gotcha The component relies on `libphonenumber-js` for its core functionality. As this package is unmaintained, it will not receive updates to `libphonenumber-js`. This can result in outdated phone number data, incorrect validation for new country codes or formats, and potential security vulnerabilities if `libphonenumber-js` has critical updates.
- gotcha The component requires a global CSS import (`import 'vue-phone-number-input/dist/vue-phone-number-input.css';`). This can lead to styling conflicts with other UI libraries or custom project styles due to its global nature and potentially generic class names.
Install
-
npm install vue-phone-number-input -
yarn add vue-phone-number-input -
pnpm add vue-phone-number-input
Imports
- VuePhoneNumberInput
const VuePhoneNumberInput = require('vue-phone-number-input');import VuePhoneNumberInput from 'vue-phone-number-input';
- CSS Styles
import 'vue-phone-number-input/src/scss/vue-phone-number-input.scss';
import 'vue-phone-number-input/dist/vue-phone-number-input.css';
- Global Component Registration
Vue.component('vue-phone-number-input', VuePhoneNumberInput.default);Vue.component('VuePhoneNumberInput', VuePhoneNumberInput);
Quickstart
import Vue from 'vue'; // Ensure Vue 2 is installed
import VuePhoneNumberInput from 'vue-phone-number-input';
import 'vue-phone-number-input/dist/vue-phone-number-input.css';
// Register the component globally (common in Vue 2 applications)
Vue.component('VuePhoneNumberInput', VuePhoneNumberInput);
// Example Vue 2 component where it would be used
const App = Vue.extend({
name: 'PhoneNumberDemo',
data() {
return {
userPhoneNumber: '+12025550100' as string, // Initial phone number value
defaultCountry: 'US' as string,
phoneDetails: null as Record<string, any> | null, // To store the output object
};
},
methods: {
onUpdate(payload: Record<string, any>) {
console.log('Phone number data:', payload);
this.phoneDetails = payload;
},
},
template: `
<div id="app-container">
<h2>Phone Number Entry (Vue 2)</h2>
<p>Current V-Model: <code>{{ userPhoneNumber }}</code></p>
<VuePhoneNumberInput
v-model="userPhoneNumber"
:default-country-code="defaultCountry"
:no-use-browser-locale="true"
:preferred-countries="['US', 'CA', 'MX', 'DE']"
@update="onUpdate"
:translations="{ countrySelectorLabel: 'Select Country', phoneNumberLabel: 'Enter Phone' }"
/>
<div v-if="phoneDetails">
<h3>Parsed Phone Details:</h3>
<pre>{{ JSON.stringify(phoneDetails, null, 2) }}</pre>
</div>
<p style="margin-top: 20px;"><em>Note: This component is unmaintained and for Vue 2 only.</em></p>
</div>
`,
});
// Mount the app (in a typical Vue 2 setup)
// new Vue({
// el: '#app',
// render: h => h(App)
// });