From 841f9a86a5ef48f00b07fdba2591023f20707541 Mon Sep 17 00:00:00 2001 From: Urvish Date: Mon, 16 Nov 2026 16:08:51 +0530 Subject: [PATCH 1/2] 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 --- package-lock.json | 7 + package.json | 1 + src/App.tsx | 12 +- src/components/auth/ProfileDrawer.tsx | 6 +- src/components/checkout/BillingPhoneModal.tsx | 15 +- src/components/common/PhoneInput.tsx | 52 ++-- src/constants/countries.ts | 189 ++++++++------- src/utils/validation.ts | 224 ++++++++++-------- 8 files changed, 279 insertions(+), 227 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3499fd6..f5ad8ea 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0", "dependencies": { "axios": "^1.7.0", + "libphonenumber-js": "^1.13.16", "lucide-react": "^0.460.0", "react": "^19.0.0", "react-dom": "^19.0.0" @@ -3455,6 +3456,12 @@ "node": ">= 0.8.0" } }, + "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/lightningcss": { "version": "1.32.0", "resolved": "https://registry.npmjs.org/lightningcss/-/lightningcss-1.32.0.tgz", diff --git a/package.json b/package.json index 52b54af..14dab7a 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ }, "dependencies": { "axios": "^1.7.0", + "libphonenumber-js": "^1.13.16", "lucide-react": "^0.460.0", "react": "^19.0.0", "react-dom": "^19.0.0" diff --git a/src/App.tsx b/src/App.tsx index af1024d..79cffc5 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -385,7 +385,7 @@ export default function App() { setProfileSaving(false); return; } - cleanedPhoneToSave = formatE164Phone(parsed.dialCode, parsed.localDigits); + cleanedPhoneToSave = formatE164Phone(parsed.dialCode, parsed.localDigits, profileCountry); } try { @@ -438,8 +438,9 @@ export default function App() { } return false; } catch (err: any) { - alert(err.response?.data?.detail || 'Failed to save billing phone number.'); - return false; + const errorMsg = err.response?.data?.detail || 'Failed to save billing phone number.'; + alert(errorMsg); + throw new Error(errorMsg); } }; @@ -477,8 +478,7 @@ export default function App() { setAuthLoading(false); return; } - const rawDigits = authPhone.replace(/\D/g, ''); - fullPhone = `${authDialCode}${rawDigits}`; + fullPhone = formatE164Phone(authDialCode, authPhone, authCountry); } const endpoint = authMode === 'login' ? '/api/auth/login' : '/api/auth/register'; @@ -887,7 +887,7 @@ export default function App() { // Sanitize contact for Razorpay: strictly E.164 (+) const parsedContact = parsePhoneNumber(loggedInUser?.phone_number, loggedInUser?.country || 'India'); - const sanitizedContact = formatE164Phone(parsedContact.dialCode, parsedContact.localDigits); + const sanitizedContact = formatE164Phone(parsedContact.dialCode, parsedContact.localDigits, loggedInUser?.country || 'India'); const rzpOptions = { key: razorpayKeyId, diff --git a/src/components/auth/ProfileDrawer.tsx b/src/components/auth/ProfileDrawer.tsx index c0bddec..0b6e59b 100644 --- a/src/components/auth/ProfileDrawer.tsx +++ b/src/components/auth/ProfileDrawer.tsx @@ -94,7 +94,7 @@ export const ProfileDrawer: React.FC = ({ const handleLocalPhoneChange = (newLocal: string) => { setLocalPhone(newLocal); - setProfilePhone(newLocal ? formatE164Phone(dialCode, newLocal) : ''); + setProfilePhone(newLocal ? formatE164Phone(dialCode, newLocal, profileCountry) : ''); }; const handleDialCodeChange = (newCode: string, newCountry: string) => { @@ -104,7 +104,7 @@ export const ProfileDrawer: React.FC = ({ setCurrency(rule.currency); const clamped = cleanPhoneInput(localPhone, rule.maxLength, false); setLocalPhone(clamped); - setProfilePhone(formatE164Phone(newCode, clamped)); + setProfilePhone(formatE164Phone(newCode, clamped, newCountry)); }; const handleCountrySelectChange = (newCountry: string) => { @@ -114,7 +114,7 @@ export const ProfileDrawer: React.FC = ({ setCurrency(rule.currency); const clamped = cleanPhoneInput(localPhone, rule.maxLength, false); setLocalPhone(clamped); - setProfilePhone(formatE164Phone(rule.code, clamped)); + setProfilePhone(formatE164Phone(rule.code, clamped, newCountry)); }; if (!isOpen || !loggedInUser) return null; diff --git a/src/components/checkout/BillingPhoneModal.tsx b/src/components/checkout/BillingPhoneModal.tsx index db4637a..925a61f 100644 --- a/src/components/checkout/BillingPhoneModal.tsx +++ b/src/components/checkout/BillingPhoneModal.tsx @@ -53,11 +53,16 @@ export const BillingPhoneModal: React.FC = ({ } setSaving(true); - const e164 = formatE164Phone(dialCode, phone); - const success = await onSavePhone(e164, country); - setSaving(false); - if (success) { - onClose(); + const e164 = formatE164Phone(dialCode, phone, country); + try { + const success = await onSavePhone(e164, country); + if (success) { + onClose(); + } + } catch (err: any) { + setError(err?.message || 'Failed to save billing phone number.'); + } finally { + setSaving(false); } }; diff --git a/src/components/common/PhoneInput.tsx b/src/components/common/PhoneInput.tsx index 311cddb..38d8ad2 100644 --- a/src/components/common/PhoneInput.tsx +++ b/src/components/common/PhoneInput.tsx @@ -27,24 +27,24 @@ export const PhoneInput: React.FC = ({ id = 'phone-input', clearOnDialCodeChange = false, }) => { - let activeRule = getCountryRuleByDialCode(dialCode); - if (!activeRule && country) { - activeRule = getCountryRuleByName(country); + // Prioritize country name if available, fallback to dial code + let activeRule = country ? getCountryRuleByName(country) : undefined; + if (!activeRule && dialCode) { + activeRule = getCountryRuleByDialCode(dialCode, country); } if (!activeRule) { activeRule = getCountryRuleByDialCode('+91')!; } - const handleDialCodeSelect = (e: React.ChangeEvent) => { - const newCode = e.target.value; - const rule = getCountryRuleByDialCode(newCode); - const newCountry = rule ? rule.country : (country || 'India'); + const handleCountrySelect = (e: React.ChangeEvent) => { + const selectedCountryName = e.target.value; + const rule = getCountryRuleByName(selectedCountryName); if (onDialCodeChange) { - onDialCodeChange(newCode, newCountry); + onDialCodeChange(rule.code, rule.country); } if (clearOnDialCodeChange) { onPhoneChange(''); - } else if (rule) { + } else { onPhoneChange(cleanPhoneInput(phone, rule.maxLength, false)); } }; @@ -58,7 +58,7 @@ export const PhoneInput: React.FC = ({ const sorted = [...COUNTRY_CODES].sort((a, b) => b.code.length - a.code.length); const matched = sorted.find((c) => trimmed.startsWith(c.code)); if (matched) { - if (onDialCodeChange && matched.code !== dialCode) { + if (onDialCodeChange && (matched.code !== dialCode || matched.country !== country)) { onDialCodeChange(matched.code, matched.country); } const rest = trimmed.slice(matched.code.length).replace(/\D/g, ''); @@ -108,11 +108,11 @@ export const PhoneInput: React.FC = ({ return (
- {/* Country Code Dropdown */} -
+ {/* Country Code Dropdown: keyed by unique country name to avoid +1 (US vs CA) collisions */} +