New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

use-phone-input

Package Overview
Dependencies
Maintainers
0
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-phone-input

Simplify phone number handling in React applications with automatic formatting, validation, and country code selection for international numbers

  • 1.1.13
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
31
decreased by-38%
Maintainers
0
Weekly downloads
 
Created
Source

use-phone-input

This library requires React v16.8 or later.

Simplify phone number handling in React applications with automatic formatting, validation, and country code selection for international numbers. This library also provides the flexibility to work exclusively with specific countries, effectively reducing the final bundle size of your application. All phone number data is sourced from the Google phone number lib. Moreover, the library is equipped with automated update checks every day.

Installation

using npm:

npm install --save use-phone-input

or yarn:

yarn add use-phone-input

or pnpm:

pnpm add use-phone-input

API

useInternationalPhoneInput

Simplify international phone number handling in React apps with automatic formatting and country code selection.

type PhoneNumber = {
  iso2: ISO2 | '';
  countryCode: string;
  nationalNumber: string;
  formattedValue: string;
};

const useInternationalPhoneInput: (
  utils: PhoneNumberUtils,
  value: PhoneNumber,
  onChange: (value: PhoneNumber) => void
) => {
  inputProps: {
    type: 'tel';
    value: string;
    onInput: FormEventHandler<HTMLInputElement>;
  };
  setCountry(iso2: ISO2): void;
};

Example:

import phoneNumberUtils from 'use-phone-input/phoneNumberUtils';
import useInternationalPhoneInput from 'use-phone-input/useInternationalPhoneInput';

// use it if you want to have no prefix in formatted phone number
phoneNumberUtils.setPrefix('');

const PhoneInput = () => {
  const [phone, setPhone] = useState(phoneNumberUtils.toPhoneNumber(''));

  const { inputProps, setCountry } = useInternationalPhoneInput(
    phoneNumberUtils,
    phone,
    setPhone
  );

  return (
    <div>
      <select
        value={phone.iso2}
        onChange={(e) => {
          setCountry(e.target.value);
        }}
      >
        <option value=''>-</option>
        {phoneNumberUtils.iso2List.map((iso2) => (
          <option key={iso2} value={iso2}>
            {iso2}
          </option>
        ))}
      </select>
      <input {...inputProps} />
    </div>
  );
};

createPhoneNumberUtils

Generate utils for specific list of countries, use it if you need to handle specific countries phone numbers, otherwise just use phoneNumberUtils, like in the example above

type PhoneNumberUtils = {
  toPhoneNumber(value: string, estimatedIso2?: ISO2): PhoneNumber;
  getCountryCode<T extends string>(
    iso2: T
  ): T extends ISO2 ? string : undefined;
  setPrefix(prefix: string): void;
  readonly iso2List: ISO2[];
};

const createPhoneNumberUtils: (
  data: PhoneNumberFormat[],
  prefix?: string // '+' by default
) => PhoneNumberUtils;

Example:

import createPhoneNumberUtils from 'use-phone-input/createPhoneNumberUtils';
import CA from 'use-phone-input/phoneNumberFormats/CA';
import US from 'use-phone-input/phoneNumberFormats/US';

// generates utils only for Canada and USA phone numbers
const phoneNumberUtils = createPhoneNumberUtils([CA, US]);

isPhoneNumberValid

Checks if the given international phone number is valid based on the provided phone verification patterns

const isPhoneNumberValid: (
  phoneValidationPatterns: Record<string, RegExp>,
  value: PhoneNumber
) => boolean;

Example:

import isPhoneNumberValid from 'use-phone-input/isPhoneNumberValid';

import phoneValidationPatterns from 'use-phone-input/phoneValidationPatterns';

import CA from 'use-phone-input/phoneValidationPatterns/CA';
import US from 'use-phone-input/phoneValidationPatterns/US';

isPhoneNumberValid(phoneValidationPatterns, value);

isPhoneNumberValid({ CA, US }, value);

isPhoneNumberValidAsync

Checks if a given international phone number is valid

This method uses dynamic import under the hood, use only if your builder supports it

const isPhoneNumberValidAsync = (value: PhoneNumber) => Promise<boolean>;

License

MIT © Krombik

Keywords

FAQs

Package last updated on 23 Jan 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc