This is a basic use case for using this library. I need to verify the number if it is valid. I use angular reactive forms custom validators.
Validator is in file: validators/phone-number.validator.ts
First step is to get google-libphonenumber PhoneNumberUtil instance
Current state of my code is:
import { ValidatorFn, AbstractControl } from '@angular/forms';
import phoneUtil = require('google-libphonenumber');
const phoneUtilInstance = phoneUtil.PhoneNumberUtil.getInstance();
export function PhoneNumberValidator(): ValidatorFn {
return (control: AbstractControl): { [key: string]: any } => {
if (!phoneUtilInstance.isValidNumber(control.value)) {
return { 'wrongNumber': { value: control.value } };
}
return null;
}
}
Usage in reactive form (file contact.component.ts
):
import { PhoneNumberValidator } from '@app/validators';
...
ngOnInit(): void { ...
this.editPhoneForm = this.formBuilder.group({
id: [''],
phone: ['', [
Validators.minLength(3),
PhoneNumberValidator()
]],
}); ...
This code can be build and executed, however, after launch I get:
ERROR TypeError: a.getCountryCodeOrDefault is not a function
at i18n.phonenumbers.PhoneNumberUtil.getRegionCodeForNumber (VM145038 libphonenumber.js:4418)
How to properly use this library in the Angular project as a validator?
Declaration
This question is similar to How to use Google libphonenumber in Typescript? but in this case it is specifically about the Angular project.