React Phone Input 2

2.15.1 · maintenance · verified Sun Apr 19

React Phone Input 2 (currently documented for version 2.15.1) is a highly customizable React component designed for international phone number input with automatic formatting. It features a country dropdown with flag icons and dynamically formats the input based on the selected country, supporting various themes like Material UI and Bootstrap. The library is built on `libphonenumber-js` for accurate validation and adheres to international standards. While this entry focuses on version 2.15.1, it's important to note that a major version 3.x exists. This library distinguishes itself through extensive customization options for appearance and behavior, making it suitable for projects requiring a polished and interactive user experience with internationalization capabilities.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates a basic international phone number input with state management, country selection, auto-formatting, and a search feature in a functional React component.

import React, { useState } from 'react';
import PhoneInput from 'react-phone-input-2';
import 'react-phone-input-2/lib/style.css'; // Or 'react-phone-input-2/lib/material.css' etc.

function MyPhoneNumberInput() {
  const [phone, setPhone] = useState('');

  const handlePhoneChange = (value, country, e, formattedValue) => {
    console.log('Raw Value:', value);
    console.log('Country Object:', country);
    console.log('Event:', e);
    console.log('Formatted Value:', formattedValue);
    setPhone(value);
  };

  return (
    <div>
      <h2>Enter Your Phone Number</h2>
      <PhoneInput
        country={'us'}
        value={phone}
        onChange={handlePhoneChange}
        inputProps={{
          name: 'phone',
          required: true,
          autoFocus: true,
        }}
        placeholder="e.g. +1 702 123 4567"
        enableSearch
        disabled={false}
      />
      <p>Current Phone Number: {phone ? `+${phone}` : 'None'}</p>
    </div>
  );
}

export default MyPhoneNumberInput;

view raw JSON →