fix(validation): add country-specific mobile phone validation and libphonenumber-js
- Integrate libphonenumber-js to validate country mobile numbering plans (e.g., France 06/07, India 6-9, UK 07) - Add ISO-2 country codes to eliminate dropdown collisions between countries sharing dial codes (+1 for US and Canada) - Automatically strip leading national trunk zero (0) when converting to standard international E.164 format - Ensure new dependency is registered in package.json and package-lock.json for npm install
This commit is contained in:
parent
02cd589cc4
commit
0c60b835a0
|
|
@ -32,6 +32,7 @@
|
|||
"expo-symbols": "~1.0.7",
|
||||
"expo-system-ui": "~6.0.9",
|
||||
"expo-web-browser": "~15.0.11",
|
||||
"libphonenumber-js": "^1.13.16",
|
||||
"react": "19.1.0",
|
||||
"react-dom": "19.1.0",
|
||||
"react-native": "0.81.5",
|
||||
|
|
@ -6170,6 +6171,12 @@
|
|||
"node": ">=6"
|
||||
}
|
||||
},
|
||||
"node_modules/libphonenumber-js": {
|
||||
"version": "1.13.16",
|
||||
"resolved": "https://registry.npmjs.org/libphonenumber-js/-/libphonenumber-js-1.13.16.tgz",
|
||||
"integrity": "sha512-tIOCM9yx2Go6d5/ZANe0K+G4AZek1Ymx81HxQpf19Sri4zMP95eQbKdnUGHAvFQ4nx0PeLhFqvAfisTADXj3tQ==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/lighthouse-logger": {
|
||||
"version": "1.4.2",
|
||||
"resolved": "https://registry.npmjs.org/lighthouse-logger/-/lighthouse-logger-1.4.2.tgz",
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@
|
|||
"expo-symbols": "~1.0.7",
|
||||
"expo-system-ui": "~6.0.9",
|
||||
"expo-web-browser": "~15.0.11",
|
||||
"libphonenumber-js": "^1.13.16",
|
||||
"react": "19.1.0",
|
||||
"react-dom": "19.1.0",
|
||||
"react-native": "0.81.5",
|
||||
|
|
|
|||
|
|
@ -1,4 +1,7 @@
|
|||
import { parsePhoneNumberFromString, validatePhoneNumberLength, type CountryCode as LibCountryCode } from "libphonenumber-js/max";
|
||||
|
||||
export interface CountryCode {
|
||||
iso2: LibCountryCode;
|
||||
code: string;
|
||||
label: string;
|
||||
country: string;
|
||||
|
|
@ -10,121 +13,115 @@ export interface CountryCode {
|
|||
|
||||
export const COUNTRY_CODES: CountryCode[] = [
|
||||
// South Asia
|
||||
{ code: "+91", label: "🇮🇳 +91", country: "India", currency: "INR", minLength: 10, maxLength: 10, placeholder: "10-digit mobile number" },
|
||||
{ code: "+92", label: "🇵🇰 +92", country: "Pakistan", currency: "USD", minLength: 10, maxLength: 10, placeholder: "10-digit mobile number" },
|
||||
{ code: "+880", label: "🇧🇩 +880", country: "Bangladesh", currency: "USD", minLength: 10, maxLength: 10, placeholder: "10-digit mobile number" },
|
||||
{ code: "+94", label: "🇱🇰 +94", country: "Sri Lanka", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ code: "+977", label: "🇳🇵 +977", country: "Nepal", currency: "USD", minLength: 10, maxLength: 10, placeholder: "10-digit mobile number" },
|
||||
{ code: "+975", label: "🇧🇹 +975", country: "Bhutan", currency: "USD", minLength: 8, maxLength: 8, placeholder: "8-digit mobile number" },
|
||||
{ code: "+960", label: "🇲🇻 +960", country: "Maldives", currency: "USD", minLength: 7, maxLength: 7, placeholder: "7-digit mobile number" },
|
||||
{ iso2: "IN", code: "+91", label: "🇮🇳 +91", country: "India", currency: "INR", minLength: 10, maxLength: 10, placeholder: "10-digit mobile number" },
|
||||
{ iso2: "PK", code: "+92", label: "🇵🇰 +92", country: "Pakistan", currency: "USD", minLength: 10, maxLength: 10, placeholder: "10-digit mobile number" },
|
||||
{ iso2: "BD", code: "+880", label: "🇧🇩 +880", country: "Bangladesh", currency: "USD", minLength: 10, maxLength: 10, placeholder: "10-digit mobile number" },
|
||||
{ iso2: "LK", code: "+94", label: "🇱🇰 +94", country: "Sri Lanka", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ iso2: "NP", code: "+977", label: "🇳🇵 +977", country: "Nepal", currency: "USD", minLength: 10, maxLength: 10, placeholder: "10-digit mobile number" },
|
||||
{ iso2: "BT", code: "+975", label: "🇧🇹 +975", country: "Bhutan", currency: "USD", minLength: 8, maxLength: 8, placeholder: "8-digit mobile number" },
|
||||
{ iso2: "MV", code: "+960", label: "🇲🇻 +960", country: "Maldives", currency: "USD", minLength: 7, maxLength: 7, placeholder: "7-digit mobile number" },
|
||||
|
||||
// North America
|
||||
{ code: "+1", label: "🇺🇸 +1", country: "United States", currency: "USD", minLength: 10, maxLength: 10, placeholder: "10-digit phone number" },
|
||||
{ code: "+1", label: "🇨🇦 +1", country: "Canada", currency: "USD", minLength: 10, maxLength: 10, placeholder: "10-digit phone number" },
|
||||
{ code: "+52", label: "🇲🇽 +52", country: "Mexico", currency: "USD", minLength: 10, maxLength: 10, placeholder: "10-digit mobile number" },
|
||||
{ iso2: "US", code: "+1", label: "🇺🇸 +1", country: "United States", currency: "USD", minLength: 10, maxLength: 10, placeholder: "10-digit phone number" },
|
||||
{ iso2: "CA", code: "+1", label: "🇨🇦 +1", country: "Canada", currency: "USD", minLength: 10, maxLength: 10, placeholder: "10-digit phone number" },
|
||||
{ iso2: "MX", code: "+52", label: "🇲🇽 +52", country: "Mexico", currency: "USD", minLength: 10, maxLength: 10, placeholder: "10-digit mobile number" },
|
||||
|
||||
// Europe & UK
|
||||
{ code: "+44", label: "🇬🇧 +44", country: "United Kingdom", currency: "USD", minLength: 10, maxLength: 11, placeholder: "10 to 11 digits" },
|
||||
{ code: "+49", label: "🇩🇪 +49", country: "Germany", currency: "EUR", minLength: 10, maxLength: 11, placeholder: "10 to 11 digits" },
|
||||
{ code: "+33", label: "🇫🇷 +33", country: "France", currency: "EUR", minLength: 9, maxLength: 10, placeholder: "9 to 10 digits" },
|
||||
{ code: "+39", label: "🇮🇹 +39", country: "Italy", currency: "EUR", minLength: 9, maxLength: 10, placeholder: "9 to 10 digits" },
|
||||
{ code: "+34", label: "🇪🇸 +34", country: "Spain", currency: "EUR", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ code: "+31", label: "🇳🇱 +31", country: "Netherlands", currency: "EUR", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ code: "+41", label: "🇨🇭 +41", country: "Switzerland", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ code: "+32", label: "🇧🇪 +32", country: "Belgium", currency: "EUR", minLength: 8, maxLength: 9, placeholder: "8 to 9 digits" },
|
||||
{ code: "+43", label: "🇦🇹 +43", country: "Austria", currency: "EUR", minLength: 10, maxLength: 11, placeholder: "10 to 11 digits" },
|
||||
{ code: "+353", label: "🇮🇪 +353", country: "Ireland", currency: "EUR", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ code: "+46", label: "🇸🇪 +46", country: "Sweden", currency: "EUR", minLength: 7, maxLength: 10, placeholder: "7 to 10 digits" },
|
||||
{ code: "+47", label: "🇳🇴 +47", country: "Norway", currency: "USD", minLength: 8, maxLength: 8, placeholder: "8-digit mobile number" },
|
||||
{ code: "+45", label: "🇩🇰 +45", country: "Denmark", currency: "EUR", minLength: 8, maxLength: 8, placeholder: "8-digit mobile number" },
|
||||
{ code: "+358", label: "🇫🇮 +358", country: "Finland", currency: "EUR", minLength: 6, maxLength: 10, placeholder: "6 to 10 digits" },
|
||||
{ code: "+351", label: "🇵🇹 +351", country: "Portugal", currency: "EUR", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ code: "+30", label: "🇬🇷 +30", country: "Greece", currency: "EUR", minLength: 10, maxLength: 10, placeholder: "10-digit mobile number" },
|
||||
{ code: "+48", label: "🇵🇱 +48", country: "Poland", currency: "EUR", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ code: "+420", label: "🇨🇿 +420", country: "Czech Republic", currency: "EUR", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ code: "+36", label: "🇭🇺 +36", country: "Hungary", currency: "EUR", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ code: "+40", label: "🇷🇴 +40", country: "Romania", currency: "EUR", minLength: 9, maxLength: 10, placeholder: "9 to 10 digits" },
|
||||
{ code: "+380", label: "🇺🇦 +380", country: "Ukraine", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ code: "+354", label: "🇮🇸 +354", country: "Iceland", currency: "EUR", minLength: 7, maxLength: 9, placeholder: "7 to 9 digits" },
|
||||
{ code: "+352", label: "🇱🇺 +352", country: "Luxembourg", currency: "EUR", minLength: 8, maxLength: 10, placeholder: "8 to 10 digits" },
|
||||
{ code: "+385", label: "🇭🇷 +385", country: "Croatia", currency: "EUR", minLength: 8, maxLength: 9, placeholder: "8 to 9 digits" },
|
||||
{ code: "+421", label: "🇸🇰 +421", country: "Slovakia", currency: "EUR", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ code: "+359", label: "🇧🇬 +359", country: "Bulgaria", currency: "EUR", minLength: 8, maxLength: 9, placeholder: "8 to 9 digits" },
|
||||
{ code: "+7", label: "🇷🇺 +7", country: "Russia", currency: "USD", minLength: 10, maxLength: 10, placeholder: "10-digit mobile number" },
|
||||
{ code: "+90", label: "🇹🇷 +90", country: "Turkey", currency: "USD", minLength: 10, maxLength: 10, placeholder: "10-digit mobile number" },
|
||||
{ iso2: "GB", code: "+44", label: "🇬🇧 +44", country: "United Kingdom", currency: "USD", minLength: 10, maxLength: 11, placeholder: "10 to 11 digits" },
|
||||
{ iso2: "DE", code: "+49", label: "🇩🇪 +49", country: "Germany", currency: "EUR", minLength: 10, maxLength: 11, placeholder: "10 to 11 digits" },
|
||||
{ iso2: "FR", code: "+33", label: "🇫🇷 +33", country: "France", currency: "EUR", minLength: 9, maxLength: 10, placeholder: "9 to 10 digits" },
|
||||
{ iso2: "IT", code: "+39", label: "🇮🇹 +39", country: "Italy", currency: "EUR", minLength: 9, maxLength: 10, placeholder: "9 to 10 digits" },
|
||||
{ iso2: "ES", code: "+34", label: "🇪🇸 +34", country: "Spain", currency: "EUR", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ iso2: "NL", code: "+31", label: "🇳🇱 +31", country: "Netherlands", currency: "EUR", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ iso2: "CH", code: "+41", label: "🇨🇭 +41", country: "Switzerland", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ iso2: "BE", code: "+32", label: "🇧🇪 +32", country: "Belgium", currency: "EUR", minLength: 8, maxLength: 9, placeholder: "8 to 9 digits" },
|
||||
{ iso2: "AT", code: "+43", label: "🇦🇹 +43", country: "Austria", currency: "EUR", minLength: 10, maxLength: 11, placeholder: "10 to 11 digits" },
|
||||
{ iso2: "IE", code: "+353", label: "🇮🇪 +353", country: "Ireland", currency: "EUR", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ iso2: "SE", code: "+46", label: "🇸🇪 +46", country: "Sweden", currency: "EUR", minLength: 7, maxLength: 10, placeholder: "7 to 10 digits" },
|
||||
{ iso2: "NO", code: "+47", label: "🇳🇴 +47", country: "Norway", currency: "USD", minLength: 8, maxLength: 8, placeholder: "8-digit mobile number" },
|
||||
{ iso2: "DK", code: "+45", label: "🇩🇰 +45", country: "Denmark", currency: "EUR", minLength: 8, maxLength: 8, placeholder: "8-digit mobile number" },
|
||||
{ iso2: "FI", code: "+358", label: "🇫🇮 +358", country: "Finland", currency: "EUR", minLength: 6, maxLength: 10, placeholder: "6 to 10 digits" },
|
||||
{ iso2: "PT", code: "+351", label: "🇵🇹 +351", country: "Portugal", currency: "EUR", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ iso2: "GR", code: "+30", label: "🇬🇷 +30", country: "Greece", currency: "EUR", minLength: 10, maxLength: 10, placeholder: "10-digit mobile number" },
|
||||
{ iso2: "PL", code: "+48", label: "🇵🇱 +48", country: "Poland", currency: "EUR", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ iso2: "CZ", code: "+420", label: "🇨🇿 +420", country: "Czech Republic", currency: "EUR", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ iso2: "HU", code: "+36", label: "🇭🇺 +36", country: "Hungary", currency: "EUR", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ iso2: "RO", code: "+40", label: "🇷🇴 +40", country: "Romania", currency: "EUR", minLength: 9, maxLength: 10, placeholder: "9 to 10 digits" },
|
||||
{ iso2: "UA", code: "+380", label: "🇺🇦 +380", country: "Ukraine", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ iso2: "IS", code: "+354", label: "🇮🇸 +354", country: "Iceland", currency: "EUR", minLength: 7, maxLength: 9, placeholder: "7 to 9 digits" },
|
||||
{ iso2: "LU", code: "+352", label: "🇱🇺 +352", country: "Luxembourg", currency: "EUR", minLength: 8, maxLength: 10, placeholder: "8 to 10 digits" },
|
||||
{ iso2: "HR", code: "+385", label: "🇭🇷 +385", country: "Croatia", currency: "EUR", minLength: 8, maxLength: 9, placeholder: "8 to 9 digits" },
|
||||
{ iso2: "SK", code: "+421", label: "🇸🇰 +421", country: "Slovakia", currency: "EUR", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ iso2: "BG", code: "+359", label: "🇧🇬 +359", country: "Bulgaria", currency: "EUR", minLength: 8, maxLength: 9, placeholder: "8 to 9 digits" },
|
||||
{ iso2: "RU", code: "+7", label: "🇷🇺 +7", country: "Russia", currency: "USD", minLength: 10, maxLength: 10, placeholder: "10-digit mobile number" },
|
||||
{ iso2: "TR", code: "+90", label: "🇹🇷 +90", country: "Turkey", currency: "USD", minLength: 10, maxLength: 10, placeholder: "10-digit mobile number" },
|
||||
|
||||
// Asia-Pacific
|
||||
{ code: "+61", label: "🇦🇺 +61", country: "Australia", currency: "USD", minLength: 9, maxLength: 10, placeholder: "9 to 10 digits" },
|
||||
{ code: "+64", label: "🇳🇿 +64", country: "New Zealand", currency: "USD", minLength: 8, maxLength: 10, placeholder: "8 to 10 digits" },
|
||||
{ code: "+81", label: "🇯🇵 +81", country: "Japan", currency: "USD", minLength: 10, maxLength: 11, placeholder: "10 to 11 digits" },
|
||||
{ code: "+86", label: "🇨🇳 +86", country: "China", currency: "USD", minLength: 11, maxLength: 11, placeholder: "11-digit mobile number" },
|
||||
{ code: "+82", label: "🇰🇷 +82", country: "South Korea", currency: "USD", minLength: 9, maxLength: 11, placeholder: "9 to 11 digits" },
|
||||
{ code: "+852", label: "🇭🇰 +852", country: "Hong Kong", currency: "USD", minLength: 8, maxLength: 8, placeholder: "8-digit mobile number" },
|
||||
{ code: "+886", label: "🇹🇼 +886", country: "Taiwan", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ code: "+65", label: "🇸🇬 +65", country: "Singapore", currency: "USD", minLength: 8, maxLength: 8, placeholder: "8-digit mobile number" },
|
||||
{ code: "+60", label: "🇲🇾 +60", country: "Malaysia", currency: "USD", minLength: 9, maxLength: 10, placeholder: "9 to 10 digits" },
|
||||
{ code: "+62", label: "🇮🇩 +62", country: "Indonesia", currency: "USD", minLength: 9, maxLength: 12, placeholder: "9 to 12 digits" },
|
||||
{ code: "+63", label: "🇵🇭 +63", country: "Philippines", currency: "USD", minLength: 10, maxLength: 10, placeholder: "10-digit mobile number" },
|
||||
{ code: "+66", label: "🇹🇭 +66", country: "Thailand", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ code: "+84", label: "🇻🇳 +84", country: "Vietnam", currency: "USD", minLength: 9, maxLength: 10, placeholder: "9 to 10 digits" },
|
||||
{ iso2: "AU", code: "+61", label: "🇦🇺 +61", country: "Australia", currency: "USD", minLength: 9, maxLength: 10, placeholder: "9 to 10 digits" },
|
||||
{ iso2: "NZ", code: "+64", label: "🇳🇿 +64", country: "New Zealand", currency: "USD", minLength: 8, maxLength: 10, placeholder: "8 to 10 digits" },
|
||||
{ iso2: "JP", code: "+81", label: "🇯🇵 +81", country: "Japan", currency: "USD", minLength: 10, maxLength: 11, placeholder: "10 to 11 digits" },
|
||||
{ iso2: "CN", code: "+86", label: "🇨🇳 +86", country: "China", currency: "USD", minLength: 11, maxLength: 11, placeholder: "11-digit mobile number" },
|
||||
{ iso2: "KR", code: "+82", label: "🇰🇷 +82", country: "South Korea", currency: "USD", minLength: 9, maxLength: 11, placeholder: "9 to 11 digits" },
|
||||
{ iso2: "HK", code: "+852", label: "🇭🇰 +852", country: "Hong Kong", currency: "USD", minLength: 8, maxLength: 8, placeholder: "8-digit mobile number" },
|
||||
{ iso2: "TW", code: "+886", label: "🇹🇼 +886", country: "Taiwan", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ iso2: "SG", code: "+65", label: "🇸🇬 +65", country: "Singapore", currency: "USD", minLength: 8, maxLength: 8, placeholder: "8-digit mobile number" },
|
||||
{ iso2: "MY", code: "+60", label: "🇲🇾 +60", country: "Malaysia", currency: "USD", minLength: 9, maxLength: 10, placeholder: "9 to 10 digits" },
|
||||
{ iso2: "ID", code: "+62", label: "🇮🇩 +62", country: "Indonesia", currency: "USD", minLength: 9, maxLength: 12, placeholder: "9 to 12 digits" },
|
||||
{ iso2: "PH", code: "+63", label: "🇵🇭 +63", country: "Philippines", currency: "USD", minLength: 10, maxLength: 10, placeholder: "10-digit mobile number" },
|
||||
{ iso2: "TH", code: "+66", label: "🇹🇭 +66", country: "Thailand", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ iso2: "VN", code: "+84", label: "🇻🇳 +84", country: "Vietnam", currency: "USD", minLength: 9, maxLength: 10, placeholder: "9 to 10 digits" },
|
||||
|
||||
// Middle East
|
||||
{ code: "+971", label: "🇦🇪 +971", country: "UAE", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ code: "+966", label: "🇸🇦 +966", country: "Saudi Arabia", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ code: "+974", label: "🇶🇦 +974", country: "Qatar", currency: "USD", minLength: 8, maxLength: 8, placeholder: "8-digit mobile number" },
|
||||
{ code: "+965", label: "🇰🇼 +965", country: "Kuwait", currency: "USD", minLength: 8, maxLength: 8, placeholder: "8-digit mobile number" },
|
||||
{ code: "+968", label: "🇴🇲 +968", country: "Oman", currency: "USD", minLength: 8, maxLength: 8, placeholder: "8-digit mobile number" },
|
||||
{ code: "+973", label: "🇧🇭 +973", country: "Bahrain", currency: "USD", minLength: 8, maxLength: 8, placeholder: "8-digit mobile number" },
|
||||
{ code: "+972", label: "🇮🇱 +972", country: "Israel", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ code: "+962", label: "🇯🇴 +962", country: "Jordan", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ code: "+961", label: "🇱🇧 +961", country: "Lebanon", currency: "USD", minLength: 7, maxLength: 8, placeholder: "7 to 8 digits" },
|
||||
{ iso2: "AE", code: "+971", label: "🇦🇪 +971", country: "UAE", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ iso2: "SA", code: "+966", label: "🇸🇦 +966", country: "Saudi Arabia", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ iso2: "QA", code: "+974", label: "🇶🇦 +974", country: "Qatar", currency: "USD", minLength: 8, maxLength: 8, placeholder: "8-digit mobile number" },
|
||||
{ iso2: "KW", code: "+965", label: "🇰🇼 +965", country: "Kuwait", currency: "USD", minLength: 8, maxLength: 8, placeholder: "8-digit mobile number" },
|
||||
{ iso2: "OM", code: "+968", label: "🇴🇲 +968", country: "Oman", currency: "USD", minLength: 8, maxLength: 8, placeholder: "8-digit mobile number" },
|
||||
{ iso2: "BH", code: "+973", label: "🇧🇭 +973", country: "Bahrain", currency: "USD", minLength: 8, maxLength: 8, placeholder: "8-digit mobile number" },
|
||||
{ iso2: "IL", code: "+972", label: "🇮🇱 +972", country: "Israel", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ iso2: "JO", code: "+962", label: "🇯🇴 +962", country: "Jordan", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ iso2: "LB", code: "+961", label: "🇱🇧 +961", country: "Lebanon", currency: "USD", minLength: 7, maxLength: 8, placeholder: "7 to 8 digits" },
|
||||
|
||||
// Latin America
|
||||
{ code: "+55", label: "🇧🇷 +55", country: "Brazil", currency: "USD", minLength: 10, maxLength: 11, placeholder: "10 to 11 digits" },
|
||||
{ code: "+54", label: "🇦🇷 +54", country: "Argentina", currency: "USD", minLength: 10, maxLength: 11, placeholder: "10 to 11 digits" },
|
||||
{ code: "+56", label: "🇨🇱 +56", country: "Chile", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ code: "+57", label: "🇨🇴 +57", country: "Colombia", currency: "USD", minLength: 10, maxLength: 10, placeholder: "10-digit mobile number" },
|
||||
{ code: "+51", label: "🇵🇪 +51", country: "Peru", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ iso2: "BR", code: "+55", label: "🇧🇷 +55", country: "Brazil", currency: "USD", minLength: 10, maxLength: 11, placeholder: "10 to 11 digits" },
|
||||
{ iso2: "AR", code: "+54", label: "🇦🇷 +54", country: "Argentina", currency: "USD", minLength: 10, maxLength: 11, placeholder: "10 to 11 digits" },
|
||||
{ iso2: "CL", code: "+56", label: "🇨🇱 +56", country: "Chile", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ iso2: "CO", code: "+57", label: "🇨🇴 +57", country: "Colombia", currency: "USD", minLength: 10, maxLength: 10, placeholder: "10-digit mobile number" },
|
||||
{ iso2: "PE", code: "+51", label: "🇵🇪 +51", country: "Peru", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
|
||||
// Africa
|
||||
{ code: "+27", label: "🇿🇦 +27", country: "South Africa", currency: "USD", minLength: 9, maxLength: 10, placeholder: "9 to 10 digits" },
|
||||
{ code: "+234", label: "🇳🇬 +234", country: "Nigeria", currency: "USD", minLength: 10, maxLength: 11, placeholder: "10 to 11 digits" },
|
||||
{ code: "+254", label: "🇰🇪 +254", country: "Kenya", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ code: "+233", label: "🇬🇭 +233", country: "Ghana", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ code: "+20", label: "🇪🇬 +20", country: "Egypt", currency: "USD", minLength: 10, maxLength: 10, placeholder: "10-digit mobile number" },
|
||||
{ code: "+212", label: "🇲🇦 +212", country: "Morocco", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ code: "+230", label: "🇲🇺 +230", country: "Mauritius", currency: "USD", minLength: 7, maxLength: 8, placeholder: "7 to 8 digits" },
|
||||
{ iso2: "ZA", code: "+27", label: "🇿🇦 +27", country: "South Africa", currency: "USD", minLength: 9, maxLength: 10, placeholder: "9 to 10 digits" },
|
||||
{ iso2: "NG", code: "+234", label: "🇳🇬 +234", country: "Nigeria", currency: "USD", minLength: 10, maxLength: 11, placeholder: "10 to 11 digits" },
|
||||
{ iso2: "KE", code: "+254", label: "🇰🇪 +254", country: "Kenya", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ iso2: "GH", code: "+233", label: "🇬🇭 +233", country: "Ghana", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ iso2: "EG", code: "+20", label: "🇪🇬 +20", country: "Egypt", currency: "USD", minLength: 10, maxLength: 10, placeholder: "10-digit mobile number" },
|
||||
{ iso2: "MA", code: "+212", label: "🇲🇦 +212", country: "Morocco", currency: "USD", minLength: 9, maxLength: 9, placeholder: "9-digit mobile number" },
|
||||
{ iso2: "MU", code: "+230", label: "🇲🇺 +230", country: "Mauritius", currency: "USD", minLength: 7, maxLength: 8, placeholder: "7 to 8 digits" },
|
||||
];
|
||||
|
||||
export const DEFAULT_COUNTRY = COUNTRY_CODES[0]; // India (+91)
|
||||
|
||||
export const SORTED_COUNTRY_CODES: CountryCode[] = [...COUNTRY_CODES].sort((a, b) =>
|
||||
a.country.localeCompare(b.country)
|
||||
);
|
||||
|
||||
export const getCountryRuleByDialCode = (dialCode: string): CountryCode => {
|
||||
return (
|
||||
COUNTRY_CODES.find((c) => c.code === dialCode) || {
|
||||
code: dialCode,
|
||||
label: dialCode,
|
||||
country: "Other",
|
||||
currency: "USD",
|
||||
minLength: 7,
|
||||
maxLength: 15,
|
||||
placeholder: "Phone number",
|
||||
}
|
||||
);
|
||||
export const DEFAULT_COUNTRY = COUNTRY_CODES[0]; // India (+91)
|
||||
|
||||
export const getCountryRuleByIso = (iso?: string | null): CountryCode => {
|
||||
if (!iso) return DEFAULT_COUNTRY;
|
||||
const upper = iso.toUpperCase().trim();
|
||||
return COUNTRY_CODES.find((c) => c.iso2 === upper) || DEFAULT_COUNTRY;
|
||||
};
|
||||
|
||||
export const getCountryRuleByName = (countryName: string): CountryCode => {
|
||||
export const getCountryRuleByName = (countryName?: string | null): CountryCode => {
|
||||
if (!countryName) return DEFAULT_COUNTRY;
|
||||
const normalized = countryName.toLowerCase().trim();
|
||||
return (
|
||||
COUNTRY_CODES.find(
|
||||
(c) =>
|
||||
c.country.toLowerCase() === normalized ||
|
||||
c.iso2.toLowerCase() === normalized ||
|
||||
normalized.includes(c.country.toLowerCase())
|
||||
) || {
|
||||
iso2: "IN",
|
||||
code: "+91",
|
||||
label: "🇮🇳 +91",
|
||||
country: countryName,
|
||||
|
|
@ -136,6 +133,32 @@ export const getCountryRuleByName = (countryName: string): CountryCode => {
|
|||
);
|
||||
};
|
||||
|
||||
export const getCountryRuleByDialCode = (dialCode?: string | null, countryHint?: string | null): CountryCode => {
|
||||
if (!dialCode) return DEFAULT_COUNTRY;
|
||||
if (countryHint) {
|
||||
const byCountry = getCountryRuleByName(countryHint);
|
||||
if (byCountry && byCountry.code === dialCode) {
|
||||
return byCountry;
|
||||
}
|
||||
}
|
||||
return (
|
||||
COUNTRY_CODES.find((c) => c.code === dialCode) || {
|
||||
iso2: "IN",
|
||||
code: dialCode,
|
||||
label: dialCode,
|
||||
country: countryHint || "Other",
|
||||
currency: "USD",
|
||||
minLength: 7,
|
||||
maxLength: 15,
|
||||
placeholder: "Phone number",
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Validates mobile phone number according to country telecom numbering plans.
|
||||
* Uses libphonenumber-js to enforce mobile prefixes (e.g., France 06/07, India 6-9, UK 07).
|
||||
*/
|
||||
export const validatePhoneNumber = (
|
||||
val?: string | null,
|
||||
dialCode?: string,
|
||||
|
|
@ -149,8 +172,8 @@ export const validatePhoneNumber = (
|
|||
return "Phone number cannot contain alphabetic characters.";
|
||||
}
|
||||
|
||||
// Strictly disallow hyphens, dashes, and invalid characters
|
||||
if (/[\-()_]/.test(trimmed)) {
|
||||
// Strictly disallow hyphens, dashes, and invalid symbols
|
||||
if (/[\-_()/[\]]/.test(trimmed)) {
|
||||
return "Phone number cannot contain hyphens or special symbols.";
|
||||
}
|
||||
|
||||
|
|
@ -164,67 +187,79 @@ export const validatePhoneNumber = (
|
|||
return "Please enter a valid phone number with numeric digits.";
|
||||
}
|
||||
|
||||
let rule = dialCode ? getCountryRuleByDialCode(dialCode) : undefined;
|
||||
if ((!rule || rule.country === "Other") && country) {
|
||||
rule = getCountryRuleByName(country);
|
||||
let rule = country ? getCountryRuleByName(country) : undefined;
|
||||
if (!rule && dialCode) {
|
||||
rule = getCountryRuleByDialCode(dialCode, country);
|
||||
}
|
||||
// If still not found and input starts with '+', match from COUNTRY_CODES
|
||||
if (!rule && trimmed.startsWith("+")) {
|
||||
const sorted = [...COUNTRY_CODES].sort((a, b) => b.code.length - a.code.length);
|
||||
rule = sorted.find((c) => trimmed.startsWith(c.code));
|
||||
}
|
||||
if (!rule || rule.country === "Other") {
|
||||
if (dialCode === "+91" || (country && country.toLowerCase() === "india")) {
|
||||
rule = getCountryRuleByDialCode("+91");
|
||||
}
|
||||
if (!rule) {
|
||||
rule = DEFAULT_COUNTRY;
|
||||
}
|
||||
|
||||
if (rule && rule.country !== "Other") {
|
||||
const iso2 = rule.iso2 || "IN";
|
||||
|
||||
// Format candidate string for libphonenumber-js
|
||||
let fullToParse = trimmed;
|
||||
if (!fullToParse.startsWith("+")) {
|
||||
const dialDigits = rule.code.replace(/\D/g, "");
|
||||
let localDigits = allDigits;
|
||||
|
||||
if (trimmed.startsWith("+")) {
|
||||
// Input includes country code prefix (e.g. +919876543210 or +9198765432)
|
||||
// Must strip the dial code to extract the actual local number
|
||||
if (allDigits.startsWith(dialDigits)) {
|
||||
localDigits = allDigits.slice(dialDigits.length);
|
||||
}
|
||||
} else if (allDigits.length > rule.maxLength && allDigits.startsWith(dialDigits)) {
|
||||
// Input was formatted with dial digits without '+' and exceeds local maxLength (e.g. 919876543210)
|
||||
localDigits = allDigits.slice(dialDigits.length);
|
||||
}
|
||||
|
||||
if (rule.minLength === rule.maxLength) {
|
||||
if (localDigits.length !== rule.minLength) {
|
||||
return `${rule.country} phone number must be exactly ${rule.minLength} digits.`;
|
||||
}
|
||||
if (allDigits.startsWith(dialDigits) && allDigits.length > rule.maxLength) {
|
||||
fullToParse = `+${allDigits}`;
|
||||
} else {
|
||||
if (localDigits.length < rule.minLength) {
|
||||
return `${rule.country} phone number must be at least ${rule.minLength} digits.`;
|
||||
}
|
||||
if (localDigits.length > rule.maxLength) {
|
||||
return `${rule.country} phone number cannot exceed ${rule.maxLength} digits.`;
|
||||
}
|
||||
fullToParse = `${rule.code}${allDigits}`;
|
||||
}
|
||||
}
|
||||
|
||||
// Specific country telecom prefix rules
|
||||
if (dialDigits === "91" || rule.country === "India") {
|
||||
if (!/^[6-9]/.test(localDigits)) {
|
||||
const parsed = parsePhoneNumberFromString(fullToParse, iso2);
|
||||
|
||||
if (parsed && parsed.isValid()) {
|
||||
const numberType = parsed.getType();
|
||||
|
||||
if (iso2 === "IN") {
|
||||
if (!/^[6-9]/.test(parsed.nationalNumber)) {
|
||||
return "India mobile number must begin with 6, 7, 8, or 9.";
|
||||
}
|
||||
} else if (iso2 === "FR") {
|
||||
if (numberType === "FIXED_LINE" || !/^[67]/.test(parsed.nationalNumber)) {
|
||||
return "France mobile number must begin with 06 or 07 (or 6 or 7).";
|
||||
}
|
||||
} else if (iso2 === "GB") {
|
||||
if (numberType === "FIXED_LINE" || !/^7/.test(parsed.nationalNumber)) {
|
||||
return "United Kingdom mobile number must begin with 07 (or 7).";
|
||||
}
|
||||
} else if (numberType === "FIXED_LINE") {
|
||||
return `Please enter a valid mobile number for ${rule.country}.`;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// General fallback ITU E.164
|
||||
if (allDigits.length < 7) {
|
||||
return "Phone number must contain at least 7 digits.";
|
||||
const nationalDigitsOnly = allDigits.startsWith(rule.code.replace(/\D/g, ""))
|
||||
? allDigits.slice(rule.code.replace(/\D/g, "").length)
|
||||
: allDigits;
|
||||
|
||||
const lenStatus = validatePhoneNumberLength(nationalDigitsOnly, iso2);
|
||||
if (lenStatus === "TOO_SHORT") {
|
||||
return `${rule.country} phone number is too short (min ${rule.minLength} digits).`;
|
||||
}
|
||||
if (allDigits.length > 15) {
|
||||
return "Phone number cannot exceed 15 digits.";
|
||||
if (lenStatus === "TOO_LONG") {
|
||||
return `${rule.country} phone number cannot exceed ${rule.maxLength} digits.`;
|
||||
}
|
||||
return null;
|
||||
if (lenStatus === "INVALID_LENGTH") {
|
||||
return rule.minLength === rule.maxLength
|
||||
? `${rule.country} phone number must be exactly ${rule.minLength} digits.`
|
||||
: `${rule.country} phone number must be between ${rule.minLength} and ${rule.maxLength} digits.`;
|
||||
}
|
||||
|
||||
if (iso2 === "FR") {
|
||||
return "France mobile number must begin with 06 or 07 (or 6 or 7).";
|
||||
}
|
||||
if (iso2 === "IN") {
|
||||
return "India mobile number must begin with 6, 7, 8, or 9.";
|
||||
}
|
||||
if (iso2 === "GB") {
|
||||
return "United Kingdom mobile number must begin with 07 (or 7).";
|
||||
}
|
||||
|
||||
return `Please enter a valid mobile number for ${rule.country} (e.g. ${rule.placeholder}).`;
|
||||
};
|
||||
|
||||
export const isValidPhoneNumber = (
|
||||
|
|
@ -241,14 +276,28 @@ export const isValidPhoneNumber = (
|
|||
export const parsePhoneNumber = (
|
||||
fullPhone?: string | null,
|
||||
fallbackCountry = "India"
|
||||
): { country: string; dialCode: string; localDigits: string } => {
|
||||
const defaultRule = getCountryRuleByName(fallbackCountry) || getCountryRuleByDialCode("+91");
|
||||
): { country: string; dialCode: string; localDigits: string; iso2: LibCountryCode } => {
|
||||
const defaultRule = getCountryRuleByName(fallbackCountry) || DEFAULT_COUNTRY;
|
||||
if (!fullPhone || !fullPhone.trim()) {
|
||||
return { country: defaultRule.country, dialCode: defaultRule.code, localDigits: "" };
|
||||
return { country: defaultRule.country, dialCode: defaultRule.code, localDigits: "", iso2: defaultRule.iso2 };
|
||||
}
|
||||
|
||||
const trimmed = fullPhone.trim();
|
||||
const parsed = parsePhoneNumberFromString(trimmed, defaultRule.iso2);
|
||||
|
||||
if (parsed) {
|
||||
const matchedRule = parsed.country
|
||||
? getCountryRuleByIso(parsed.country)
|
||||
: getCountryRuleByDialCode(`+${parsed.countryCallingCode}`, fallbackCountry);
|
||||
|
||||
return {
|
||||
country: matchedRule.country,
|
||||
dialCode: `+${parsed.countryCallingCode}`,
|
||||
localDigits: parsed.nationalNumber,
|
||||
iso2: matchedRule.iso2,
|
||||
};
|
||||
}
|
||||
const trimmed = fullPhone.trim();
|
||||
|
||||
// Try matching dial code prefixes from longest to shortest
|
||||
const sorted = [...COUNTRY_CODES].sort((a, b) => b.code.length - a.code.length);
|
||||
const matched = sorted.find((c) => trimmed.startsWith(c.code));
|
||||
if (matched) {
|
||||
|
|
@ -257,34 +306,43 @@ export const parsePhoneNumber = (
|
|||
country: matched.country,
|
||||
dialCode: matched.code,
|
||||
localDigits: rawLocal.slice(0, matched.maxLength),
|
||||
iso2: matched.iso2,
|
||||
};
|
||||
}
|
||||
|
||||
// If no '+' prefix, check if digits start with fallback country dial digits and exceed national length
|
||||
const dialDigits = defaultRule.code.replace(/\D/g, "");
|
||||
const rawDigits = trimmed.replace(/\D/g, "");
|
||||
let local = rawDigits;
|
||||
if (
|
||||
dialDigits &&
|
||||
rawDigits.startsWith(dialDigits) &&
|
||||
rawDigits.length > defaultRule.maxLength
|
||||
) {
|
||||
local = rawDigits.slice(dialDigits.length);
|
||||
}
|
||||
return {
|
||||
country: defaultRule.country,
|
||||
dialCode: defaultRule.code,
|
||||
localDigits: local.slice(0, defaultRule.maxLength),
|
||||
localDigits: rawDigits.slice(0, defaultRule.maxLength),
|
||||
iso2: defaultRule.iso2,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Formats full phone number cleanly as E.164 without spaces (+<dialCode><digits>)
|
||||
* Formats full phone number cleanly as E.164 without spaces (+<dialCode><digits>),
|
||||
* automatically stripping any national trunk '0' where appropriate.
|
||||
*/
|
||||
export const formatE164Phone = (dialCode: string, localDigits: string): string => {
|
||||
const digits = localDigits.replace(/\D/g, "");
|
||||
export const formatE164Phone = (dialCode: string, localDigits: string, country?: string): string => {
|
||||
if (!localDigits || !localDigits.trim()) return "";
|
||||
const rule = country ? getCountryRuleByName(country) : getCountryRuleByDialCode(dialCode, country);
|
||||
const rawDigits = localDigits.replace(/\D/g, "");
|
||||
if (!rawDigits) return "";
|
||||
|
||||
const code = dialCode.startsWith("+") ? dialCode : `+${dialCode}`;
|
||||
return digits ? `${code}${digits}` : "";
|
||||
const candidate = localDigits.startsWith("+") ? localDigits : `${code}${rawDigits}`;
|
||||
|
||||
const parsed = parsePhoneNumberFromString(candidate, rule.iso2);
|
||||
if (parsed && parsed.isValid()) {
|
||||
return parsed.format("E.164");
|
||||
}
|
||||
|
||||
let cleanLocal = rawDigits;
|
||||
if (rule.iso2 !== "IT" && cleanLocal.startsWith("0")) {
|
||||
cleanLocal = cleanLocal.replace(/^0+/, "");
|
||||
}
|
||||
|
||||
return `${code}${cleanLocal}`;
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
@ -316,35 +374,13 @@ export const updatePhoneDialCode = (
|
|||
const oldRule = getCountryRuleByName(oldCountry);
|
||||
const newRule = getCountryRuleByName(newCountry);
|
||||
|
||||
// Extract national digits without any dial code
|
||||
let nationalDigits = "";
|
||||
if (trimmed.startsWith("+")) {
|
||||
const matched = [...COUNTRY_CODES]
|
||||
.sort((a, b) => b.code.length - a.code.length)
|
||||
.find((c) => trimmed.startsWith(c.code));
|
||||
if (matched) {
|
||||
nationalDigits = trimmed.slice(matched.code.length).replace(/\D/g, "");
|
||||
} else {
|
||||
nationalDigits = trimmed.replace(/^\+/, "").replace(/\D/g, "");
|
||||
}
|
||||
} else {
|
||||
const oldDialDigits = oldRule.code.replace(/\D/g, "");
|
||||
const rawDigits = trimmed.replace(/\D/g, "");
|
||||
if (oldDialDigits && rawDigits.startsWith(oldDialDigits) && rawDigits.length > oldRule.maxLength) {
|
||||
nationalDigits = rawDigits.slice(oldDialDigits.length);
|
||||
} else {
|
||||
nationalDigits = rawDigits;
|
||||
}
|
||||
}
|
||||
|
||||
const bounded = nationalDigits.slice(0, newRule.maxLength);
|
||||
const parsed = parsePhoneNumber(trimmed, oldRule.country);
|
||||
const bounded = parsed.localDigits.slice(0, newRule.maxLength);
|
||||
return bounded ? `${newRule.code} ${bounded}` : `${newRule.code} `;
|
||||
};
|
||||
|
||||
/**
|
||||
* RFC 5322 & RFC 1035 compliant email validation regex.
|
||||
* Disallows invalid domain characters (e.g. + in domain like +gmail+.com, consecutive dots, leading/trailing hyphens)
|
||||
* while properly accepting plus-addressing in local-part (e.g. sanskar+sarak@gmail.com).
|
||||
*/
|
||||
export const EMAIL_REGEX = /^[a-zA-Z0-9_%+-]+(?:\.[a-zA-Z0-9_%+-]+)*@[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?)*\.[a-zA-Z]{2,}$/;
|
||||
|
||||
|
|
|
|||
|
|
@ -25,9 +25,11 @@ import { AuthColors } from "../constants/colors";
|
|||
import { Colors } from "@/theme";
|
||||
import {
|
||||
CountryCode,
|
||||
COUNTRY_CODES,
|
||||
DEFAULT_COUNTRY,
|
||||
SORTED_COUNTRY_CODES,
|
||||
validatePhoneNumber,
|
||||
formatE164Phone,
|
||||
validateEmail,
|
||||
} from "@/constants/countries";
|
||||
import { useAuthStore } from "@/store/auth.store";
|
||||
|
|
@ -175,7 +177,7 @@ export default function SignUpForm() {
|
|||
setIsLoading(true);
|
||||
try {
|
||||
const formattedPhone = phone.trim()
|
||||
? `${selectedCountry.code}${phone.trim()}`
|
||||
? formatE164Phone(selectedCountry.code, phone.trim(), selectedCountry.country)
|
||||
: undefined;
|
||||
|
||||
const response = await register({
|
||||
|
|
|
|||
|
|
@ -141,7 +141,7 @@ export default function EditProfileForm({
|
|||
if (trimmedPhone) {
|
||||
const rule = getCountryRuleByName(country);
|
||||
const parsed = parsePhoneNumber(trimmedPhone, country);
|
||||
normalizedPhone = formatE164Phone(rule.code, parsed.localDigits);
|
||||
normalizedPhone = formatE164Phone(rule.code, parsed.localDigits, country);
|
||||
}
|
||||
|
||||
onSave?.(trimmedName, normalizedPhone, country);
|
||||
|
|
|
|||
Loading…
Reference in New Issue