Skip to main content

Utility Functions

These are exported alongside the PhoneInput component for use outside the component (e.g. backend validation, custom parsing).

FunctionParametersReturn TypeDescription
getAllCountries()ICountry[]Returns an array of all available countries
getNationalPhoneNumber(internationalPhoneNumber: string)stringReturns the national phone number from an international phone number
getCountriesByCallingCode(callingCode: string)ICountry[] | undefinedReturns countries that match the given calling code
getCountryByCca2(cca2: string)ICountry | undefinedReturns a country by its ISO 3166-1 alpha-2 code
getCountriesByName(name: string, language?: ILanguage)ICountry[]Returns countries that match the given name in the specified language
getCountryByPhoneNumber(phoneNumber: string)ICountry | undefinedReturns the country that matches the given phone number
isValidPhoneNumber(phoneNumber: string, country: ICountry)booleanValidates if a phone number is valid for the given country
getInternationalPhoneNumberLength(country: ICountry, phoneNumber: string)numberReturns total digits of calling code + phone number
getPhoneNumberType(phoneNumber: string)PhoneNumberType | nullReturns the line type (MOBILE, FIXED_LINE, TOLL_FREE, etc) of an E.164 number, or null when invalid

Example

import {
getCountryByCca2,
isValidPhoneNumber,
getPhoneNumberType,
} from 'rn-international-phone-number';

const us = getCountryByCca2('US');
isValidPhoneNumber('2025550123', us!); // true
getPhoneNumberType('+12025550123'); // 'FIXED_LINE_OR_MOBILE'