Compare commits
2 Commits
c07bb6b52f
...
d6b2a37dde
| Author | SHA1 | Date |
|---|---|---|
|
|
d6b2a37dde | |
|
|
b65249e075 |
|
|
@ -365,7 +365,14 @@ export default function App() {
|
||||||
setProfileSaving(false);
|
setProfileSaving(false);
|
||||||
return;
|
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 {
|
try {
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ import {
|
||||||
} from 'lucide-react';
|
} from 'lucide-react';
|
||||||
import type { UserProfile, ModuleItem, Currency } from '../../types';
|
import type { UserProfile, ModuleItem, Currency } from '../../types';
|
||||||
import { COUNTRY_CODES, getCountryRuleByName } from '../../constants/countries';
|
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 {
|
interface ProfileDrawerProps {
|
||||||
isOpen: boolean;
|
isOpen: boolean;
|
||||||
|
|
@ -148,9 +148,9 @@ export const ProfileDrawer: React.FC<ProfileDrawerProps> = ({
|
||||||
<input
|
<input
|
||||||
type="tel"
|
type="tel"
|
||||||
value={profilePhone}
|
value={profilePhone}
|
||||||
onChange={(e) => setProfilePhone(cleanPhoneInput(e.target.value, 15, true))}
|
onChange={(e) => setProfilePhone(cleanPhoneInput(e.target.value, activeRule.maxLength + activeRule.code.length + 1, true))}
|
||||||
placeholder={activeRule.placeholder}
|
placeholder={activeRule.placeholder ? `e.g. ${activeRule.code} ${activeRule.placeholder}` : `e.g. ${activeRule.code} 9876543210`}
|
||||||
maxLength={16}
|
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"
|
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">
|
<p className="text-[10px] text-slate-400 font-medium">
|
||||||
|
|
@ -166,9 +166,13 @@ export const ProfileDrawer: React.FC<ProfileDrawerProps> = ({
|
||||||
value={profileCountry}
|
value={profileCountry}
|
||||||
onChange={(e) => {
|
onChange={(e) => {
|
||||||
const newCountry = e.target.value;
|
const newCountry = e.target.value;
|
||||||
setProfileCountry(newCountry);
|
if (newCountry !== profileCountry) {
|
||||||
const rule = getCountryRuleByName(newCountry);
|
const updatedPhone = updatePhoneDialCode(profilePhone, profileCountry, newCountry);
|
||||||
setCurrency(rule.currency);
|
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"
|
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
|
* 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.';
|
return 'Phone number cannot contain alphabetic characters.';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Strictly enforce numeric digits with optional leading '+' and standard formatting separators
|
// Strictly disallow hyphens, dashes, and invalid characters
|
||||||
if (!/^\+?[\d\s\-()]+$/.test(trimmed)) {
|
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.';
|
return 'Please enter a valid phone number containing only numeric digits.';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -99,6 +104,45 @@ export const isValidPhoneNumber = (
|
||||||
return validatePhoneNumber(val, dialCode, country) === null;
|
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 = /[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?`~]/;
|
export const SPECIAL_CHAR_REGEX = /[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?`~]/;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue