intl-tel-input: International Telephone Input

27.0.11 · active · verified Sun Apr 19

The `intl-tel-input` library is a robust JavaScript plugin designed for entering, formatting, and validating international telephone numbers. Currently at version 27.0.11, it maintains an active release cadence with frequent bug fixes and feature enhancements, including recent updates for React, Vue, and Svelte components, and fixes for TypeScript type resolution. Key differentiators include automatic country detection via IP lookup, placeholder examples for selected countries, keyboard navigation for the country dropdown, real-time number formatting, and comprehensive validation powered by Google's libphonenumber library. It provides high-resolution flag images, includes TypeScript definitions, and supports extensive customization through options, methods, events, and CSS variables for theming.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart initializes the international telephone input plugin on an HTML input element. It configures basic options like initial and preferred countries, loads the essential utility script for validation, and demonstrates how to handle country changes and validate the entered phone number.

import intlTelInput from 'intl-tel-input';
import 'intl-tel-input/styles'; // Import default styles

document.addEventListener('DOMContentLoaded', () => {
  const inputElement = document.getElementById('phone-input') as HTMLInputElement | null;

  if (inputElement) {
    const iti = intlTelInput(inputElement, {
      initialCountry: 'us',
      separateDialCode: true,
      preferredCountries: ['us', 'gb', 'ca'],
      // `utilsScript` is crucial for validation and formatting functions
      // It can be served locally or via a CDN.
      utilsScript: 'https://cdn.jsdelivr.net/npm/intl-tel-input@27.0.11/build/js/utils.js',
    });

    inputElement.addEventListener('countrychange', () => {
      const selectedCountryData = iti.getSelectedCountryData();
      console.log('Country changed:', selectedCountryData);
    });

    document.getElementById('validate-btn')?.addEventListener('click', () => {
      if (iti.isValidNumber()) {
        console.log('Valid number:', iti.getNumber());
        alert(`Valid number: ${iti.getNumber()}`);
      } else {
        const errorCode = iti.getValidationError();
        console.log('Invalid number (error code):', errorCode);
        alert(`Invalid number. Error code: ${errorCode}`);
      }
    });

    console.log('intl-tel-input initialized and ready.', iti);
  }
});

/*
Minimal HTML to run this code:
<input type="tel" id="phone-input">
<button id="validate-btn">Validate Number</button>
*/

view raw JSON →