
Security News
Open Source CAI Framework Handles Pen Testing Tasks up to 3,600× Faster Than Humans
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
use-phone-input
Advanced tools
Simplify phone number handling in React applications with automatic formatting, validation, and country code selection for international numbers
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.
using npm:
npm install --save use-phone-input
or yarn:
yarn add use-phone-input
or pnpm:
pnpm add use-phone-input
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>
);
};
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]);
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);
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>;
MIT © Krombik
FAQs
Simplify phone number handling in React applications with automatic formatting, validation, and country code selection for international numbers
The npm package use-phone-input receives a total of 70 weekly downloads. As such, use-phone-input popularity was classified as not popular.
We found that use-phone-input demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.
Security News
CVEForecast.org uses machine learning to project a record-breaking surge in vulnerability disclosures in 2025.