fix(profile): sync country dial code and prevent hyphens in phone numbers
This commit is contained in:
parent
8dc9d871d9
commit
b65249e075
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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<ProfileDrawerProps> = ({
|
|||
<input
|
||||
type="tel"
|
||||
value={profilePhone}
|
||||
onChange={(e) => 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"
|
||||
/>
|
||||
<p className="text-[10px] text-slate-400 font-medium">
|
||||
|
|
@ -166,9 +166,13 @@ export const ProfileDrawer: React.FC<ProfileDrawerProps> = ({
|
|||
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"
|
||||
>
|
||||
|
|
|
|||
|
|
@ -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 = /[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?`~]/;
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in New Issue