Files
zitadel/console/src/app/utils/formatPhone.ts
T
Miguel CabrerizoandGitHub 5704c44117 feat(console): phone number validation with flags (#5139)
Formats the phonenumber according to the preselected country
2023-02-02 09:36:43 +01:00

20 lines
550 B
TypeScript

import { CountryCode, parsePhoneNumber } from 'libphonenumber-js';
export function formatPhone(phone: string): { phone: string; country: CountryCode } {
const defaultCountry = 'CH';
if (phone) {
try {
const phoneNumber = parsePhoneNumber(phone, defaultCountry);
const country = phoneNumber.country ?? defaultCountry;
if (phoneNumber) {
return { phone: phoneNumber.formatInternational(), country };
}
} catch (error) {
console.error(error);
}
}
return { phone, country: defaultCountry };
}