angularvalidationphone-numberlibphonenumberngx-international-phone-number

ERROR TypeError: a.getCountryCodeOrDefault is not a function


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??


Solution

  • 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');