ERROR TypeError: a.getCountryCodeOrDefault is not a function

1.3k Views Asked by At

I'm trying to use google-libphonenumber package in an Angular project for validating phone number

import { PhoneNumberUtil } from 'google-libphonenumber';

  public phoneUtil = PhoneNumberUtil.getInstance();

  phoneUtil.isValidNumber('+919421013328');

but it gives me

TypeError: a.getCountryCodeOrDefault is not a function.

can someone please help me with this??

1

There are 1 best solutions below

1
On BEST ANSWER

From the google-libphonenumber package readme:

Most libphonenumber functions expect to receive an instance of libphonenumber.PhoneNumber which can be obtained by calling phoneUtil.parse or phoneUtil.parseAndKeepRawInput on a raw (string) number, otherwise it will throw errors like TypeError: a.getCountryCodeOrDefault is not a function.

phoneUtil.isValidNumberForRegion(phoneUtil.parse('202-456-1414', 'US'), 'US');

or if you have multiple country codes:

phoneUtil.parseAndKeepRawInput('+919421013329');