feat(console): phone number validation with flags (#5139)

Formats the phonenumber according to the preselected country
This commit is contained in:
Miguel Cabrerizo
2023-02-02 09:36:43 +01:00
committed by GitHub
parent e9d5d1dcaf
commit 5704c44117
284 changed files with 306 additions and 65 deletions
+19
View File
@@ -0,0 +1,19 @@
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 };
}