Skip to content

parsePhoneNumber

function parsePhoneNumber(input: string, options?: ParsePhoneNumberOptions): PhoneNumber;

Parses a phone number to validate, format, and inspect it. A leading + reads as international; otherwise the digits resolve against defaultRegion, with the region’s IDD prefix, a redundant leading calling code, and the national prefix all recognized and stripped. Spacing and punctuation are ignored. It never throws on bad input; the returned PhoneNumber reports why it is not valid. Like every entry point, it requires a loaded engine and throws EngineNotReadyError before that.

parsePhoneNumber('+1 (415) 555-0132').formatE164(); // '+14155550132'
parsePhoneNumber('(415) 555-0132', { defaultRegion: 'US' }).isValid(); // true

A trailing extension is captured and split off before the digits resolve, under the notations Google libphonenumber recognizes, among them ext., extension, x, #, int, comma, tilde, and the RFC 3966 ;ext= parameter. The captured digits surface through getExtension and travel through the formats; validity is judged on the base number alone.

parsePhoneNumber('+1 415 555 0132 ext. 22').isValid(); // true
parsePhoneNumber('+1 415 555 0132 x22').getExtension(); // '22'
parsePhoneNumber('tel:+1-415-555-0132;ext=22').formatE164(); // '+14155550132'
interface ParsePhoneNumberOptions {
defaultRegion?: RegionCode;
strict?: boolean;
}
defaultRegion?: RegionCode;

The region that a number without a + resolves against. Without it, those digits read as international, calling code first:

parsePhoneNumber('415-555-0132').getRegion(); // 'CH', 41 read as the calling code
parsePhoneNumber('415-555-0132', { defaultRegion: 'US' }).getRegion(); // 'US'
strict?: boolean;

Restricts validity to defaultRegion. isValid, getNumberType, and getValidationError all follow it while the possibility checks and the formats ignore it. Without defaultRegion it has no effect. It is off by default.

const number = parsePhoneNumber('(604) 555-0132', { defaultRegion: 'US', strict: true });
number.getRegion(); // 'CA'
number.isValid(); // false
number.getNumberType(); // 'UNKNOWN'

Conformance for the returned PhoneNumber’s methods is on PhoneNumber.