I am using ng2-validation which uses libphonenumber-js to validate phone numbers. I would like to allow both US and Canadian phone numbers in a phone number form control. I am currently passing CustomValidators.phone('US')
as the form control validator, which allows US phone numbers but disallows Canadian phone numbers.
Is there a way to allow both US and Canadian phone numbers in the form control with this validation method?
Looking at the source code from the validator function you're using:
You should be able to combine these on your own with an or (something along the lines of this):
Then inside of your validator, you can pass a list of country codes:
I made a new file
customPhoneValidator.ts
containing the following:In the component which uses the validator, I declared
const customPhoneCountries: CountryCode[] = ['US', 'CA'];
and passedcustomPhoneValidator(customPhoneCountries)
as a validator for the form control.