From b65249e075c3dcbc82405970446567c464ef3d75 Mon Sep 17 00:00:00 2001 From: Urvish Date: Thu, 29 Oct 2026 14:10:04 +0530 Subject: [PATCH] fix(profile): sync country dial code and prevent hyphens in phone numbers --- src/App.tsx | 9 ++++- src/components/auth/ProfileDrawer.tsx | 18 ++++++---- src/utils/validation.ts | 50 +++++++++++++++++++++++++-- 3 files changed, 66 insertions(+), 11 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index a5c5d6b..43e6cf1 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -365,7 +365,14 @@ export default function App() { setProfileSaving(false); return; } - cleanedPhoneToSave = cleanPhoneInput(profilePhone.trim(), 16, true); + const rule = getCountryRuleByName(profileCountry); + const digitsOnly = profilePhone.trim().replace(/\D/g, ''); + const dialDigits = rule.code.replace(/\D/g, ''); + let localDigits = digitsOnly; + if (digitsOnly.startsWith(dialDigits) && digitsOnly.length > dialDigits.length) { + localDigits = digitsOnly.slice(dialDigits.length); + } + cleanedPhoneToSave = `${rule.code}${localDigits}`; } try { diff --git a/src/components/auth/ProfileDrawer.tsx b/src/components/auth/ProfileDrawer.tsx index 3f40f5c..3937cfb 100644 --- a/src/components/auth/ProfileDrawer.tsx +++ b/src/components/auth/ProfileDrawer.tsx @@ -5,7 +5,7 @@ import { } from 'lucide-react'; import type { UserProfile, ModuleItem, Currency } from '../../types'; import { COUNTRY_CODES, getCountryRuleByName } from '../../constants/countries'; -import { cleanPhoneInput, SPECIAL_CHAR_REGEX } from '../../utils/validation'; +import { cleanPhoneInput, updatePhoneDialCode, SPECIAL_CHAR_REGEX } from '../../utils/validation'; interface ProfileDrawerProps { isOpen: boolean; @@ -148,9 +148,9 @@ export const ProfileDrawer: React.FC = ({ setProfilePhone(cleanPhoneInput(e.target.value, 15, true))} - placeholder={activeRule.placeholder} - maxLength={16} + onChange={(e) => setProfilePhone(cleanPhoneInput(e.target.value, activeRule.maxLength + activeRule.code.length + 1, true))} + placeholder={activeRule.placeholder ? `e.g. ${activeRule.code} ${activeRule.placeholder}` : `e.g. ${activeRule.code} 9876543210`} + maxLength={activeRule.maxLength + activeRule.code.length + 2} className="w-full bg-white border border-slate-200 rounded-xl px-3 py-2 text-xs font-semibold text-slate-800 focus:outline-none focus:border-indigo-500 focus:ring-1 focus:ring-indigo-500" />

@@ -166,9 +166,13 @@ export const ProfileDrawer: React.FC = ({ value={profileCountry} onChange={(e) => { const newCountry = e.target.value; - setProfileCountry(newCountry); - const rule = getCountryRuleByName(newCountry); - setCurrency(rule.currency); + if (newCountry !== profileCountry) { + const updatedPhone = updatePhoneDialCode(profilePhone, profileCountry, newCountry); + setProfilePhone(updatedPhone); + setProfileCountry(newCountry); + const rule = getCountryRuleByName(newCountry); + setCurrency(rule.currency); + } }} className="w-full bg-white border border-slate-200 rounded-xl px-3 py-2 text-xs font-semibold text-slate-800 focus:outline-none focus:border-indigo-500" > diff --git a/src/utils/validation.ts b/src/utils/validation.ts index 9876a97..bcfa008 100644 --- a/src/utils/validation.ts +++ b/src/utils/validation.ts @@ -1,4 +1,4 @@ -import { getCountryRuleByDialCode, getCountryRuleByName } from '../constants/countries'; +import { COUNTRY_CODES, getCountryRuleByDialCode, getCountryRuleByName } from '../constants/countries'; /** * Strips non-numeric / non-phone characters from text in real-time @@ -31,8 +31,13 @@ export const validatePhoneNumber = ( return 'Phone number cannot contain alphabetic characters.'; } - // Strictly enforce numeric digits with optional leading '+' and standard formatting separators - if (!/^\+?[\d\s\-()]+$/.test(trimmed)) { + // Strictly disallow hyphens, dashes, and invalid characters + if (/[\-()_]/.test(trimmed)) { + return 'Phone number cannot contain hyphens or special symbols.'; + } + + // Strictly enforce numeric digits with optional leading '+' and whitespace + if (!/^\+?[\d\s]+$/.test(trimmed)) { return 'Please enter a valid phone number containing only numeric digits.'; } @@ -99,6 +104,45 @@ export const isValidPhoneNumber = ( return validatePhoneNumber(val, dialCode, country) === null; }; +/** + * Updates the dial code prefix of an existing phone number when country changes. + * Automatically clamps remaining digits to the new country's maxLength. + */ +export const updatePhoneDialCode = ( + currentPhone: string, + oldCountry: string, + newCountry: string +): string => { + const trimmed = currentPhone.trim(); + if (!trimmed) return ''; + + const oldRule = getCountryRuleByName(oldCountry); + const newRule = getCountryRuleByName(newCountry); + + 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 > oldDialDigits.length) { + nationalDigits = rawDigits.slice(oldDialDigits.length); + } else { + nationalDigits = rawDigits; + } + } + + const bounded = nationalDigits.slice(0, newRule.maxLength); + return bounded ? `${newRule.code} ${bounded}` : `${newRule.code} `; +}; + export const SPECIAL_CHAR_REGEX = /[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?`~]/; /** -- 2.34.1