Merge pull request 'fix(auth): enforce strict national phone length and prevent country code combining in mobile app' (#122) from fix/standardized-phone-validation into main
Reviewed-on: https://git.zedobject.local/AcuPedia/acupedia-mobile-ui/pulls/122 Reviewed-by: Amit Salunke <amit@noreply.localhost>
This commit is contained in:
commit
4c3730a550
|
|
@ -168,6 +168,11 @@ export const validatePhoneNumber = (
|
||||||
if ((!rule || rule.country === "Other") && country) {
|
if ((!rule || rule.country === "Other") && country) {
|
||||||
rule = getCountryRuleByName(country);
|
rule = getCountryRuleByName(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 (!rule || rule.country === "Other") {
|
||||||
if (dialCode === "+91" || (country && country.toLowerCase() === "india")) {
|
if (dialCode === "+91" || (country && country.toLowerCase() === "india")) {
|
||||||
rule = getCountryRuleByDialCode("+91");
|
rule = getCountryRuleByDialCode("+91");
|
||||||
|
|
@ -178,12 +183,14 @@ export const validatePhoneNumber = (
|
||||||
const dialDigits = rule.code.replace(/\D/g, "");
|
const dialDigits = rule.code.replace(/\D/g, "");
|
||||||
let localDigits = allDigits;
|
let localDigits = allDigits;
|
||||||
|
|
||||||
// If formatted with country code (e.g. +919876543210 or 919876543210 where total length > rule.maxLength), strip country prefix
|
if (trimmed.startsWith("+")) {
|
||||||
if (
|
// Input includes country code prefix (e.g. +919876543210 or +9198765432)
|
||||||
(trimmed.startsWith("+") || allDigits.length > rule.maxLength) &&
|
// Must strip the dial code to extract the actual local number
|
||||||
allDigits.startsWith(dialDigits) &&
|
if (allDigits.startsWith(dialDigits)) {
|
||||||
allDigits.length === dialDigits.length + rule.minLength
|
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);
|
localDigits = allDigits.slice(dialDigits.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -199,6 +206,14 @@ export const validatePhoneNumber = (
|
||||||
return `${rule.country} phone number cannot exceed ${rule.maxLength} digits.`;
|
return `${rule.country} phone number cannot exceed ${rule.maxLength} digits.`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Specific country telecom prefix rules
|
||||||
|
if (dialDigits === "91" || rule.country === "India") {
|
||||||
|
if (!/^[6-9]/.test(localDigits)) {
|
||||||
|
return "India mobile number must begin with 6, 7, 8, or 9.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -220,6 +235,71 @@ export const isValidPhoneNumber = (
|
||||||
return validatePhoneNumber(val, dialCode, country) === null;
|
return validatePhoneNumber(val, dialCode, country) === null;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Splits a full phone number into dialCode, country, and localDigits
|
||||||
|
*/
|
||||||
|
export const parsePhoneNumber = (
|
||||||
|
fullPhone?: string | null,
|
||||||
|
fallbackCountry = "India"
|
||||||
|
): { country: string; dialCode: string; localDigits: string } => {
|
||||||
|
const defaultRule = getCountryRuleByName(fallbackCountry) || getCountryRuleByDialCode("+91");
|
||||||
|
if (!fullPhone || !fullPhone.trim()) {
|
||||||
|
return { country: defaultRule.country, dialCode: defaultRule.code, localDigits: "" };
|
||||||
|
}
|
||||||
|
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) {
|
||||||
|
const rawLocal = trimmed.slice(matched.code.length).replace(/\D/g, "");
|
||||||
|
return {
|
||||||
|
country: matched.country,
|
||||||
|
dialCode: matched.code,
|
||||||
|
localDigits: rawLocal.slice(0, matched.maxLength),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Formats full phone number cleanly as E.164 without spaces (+<dialCode><digits>)
|
||||||
|
*/
|
||||||
|
export const formatE164Phone = (dialCode: string, localDigits: string): string => {
|
||||||
|
const digits = localDigits.replace(/\D/g, "");
|
||||||
|
const code = dialCode.startsWith("+") ? dialCode : `+${dialCode}`;
|
||||||
|
return digits ? `${code}${digits}` : "";
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Strips non-numeric / non-phone characters from text in real-time
|
||||||
|
* If maxLength is provided, strictly restricts digit length
|
||||||
|
* If allowPlus is false, strips any '+' symbols as well (for local number inputs)
|
||||||
|
*/
|
||||||
|
export const cleanPhoneInput = (text: string, maxLength = 16, allowPlus = true): string => {
|
||||||
|
const trimmedStart = text.trimStart();
|
||||||
|
const hasPlus = allowPlus && trimmedStart.startsWith("+");
|
||||||
|
const rest = hasPlus ? trimmedStart.slice(1) : text;
|
||||||
|
const digitsOnly = rest.replace(/\D/g, "");
|
||||||
|
const boundedDigits = maxLength ? digitsOnly.slice(0, maxLength) : digitsOnly;
|
||||||
|
return hasPlus ? `+${boundedDigits}` : boundedDigits;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the dial code prefix of an existing phone number when country changes.
|
* Updates the dial code prefix of an existing phone number when country changes.
|
||||||
|
|
@ -250,7 +330,7 @@ export const updatePhoneDialCode = (
|
||||||
} else {
|
} else {
|
||||||
const oldDialDigits = oldRule.code.replace(/\D/g, "");
|
const oldDialDigits = oldRule.code.replace(/\D/g, "");
|
||||||
const rawDigits = trimmed.replace(/\D/g, "");
|
const rawDigits = trimmed.replace(/\D/g, "");
|
||||||
if (oldDialDigits && rawDigits.startsWith(oldDialDigits) && rawDigits.length > oldDialDigits.length) {
|
if (oldDialDigits && rawDigits.startsWith(oldDialDigits) && rawDigits.length > oldRule.maxLength) {
|
||||||
nationalDigits = rawDigits.slice(oldDialDigits.length);
|
nationalDigits = rawDigits.slice(oldDialDigits.length);
|
||||||
} else {
|
} else {
|
||||||
nationalDigits = rawDigits;
|
nationalDigits = rawDigits;
|
||||||
|
|
|
||||||
|
|
@ -65,6 +65,37 @@ export default function SignUpForm() {
|
||||||
}, [countrySearch]);
|
}, [countrySearch]);
|
||||||
|
|
||||||
const handlePhoneChange = (text: string) => {
|
const handlePhoneChange = (text: string) => {
|
||||||
|
const trimmed = text.trim();
|
||||||
|
|
||||||
|
// Check if user pasted an international number with '+' prefix
|
||||||
|
if (trimmed.startsWith("+")) {
|
||||||
|
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 (matched.country !== selectedCountry.country) {
|
||||||
|
setSelectedCountry(matched);
|
||||||
|
}
|
||||||
|
const rest = trimmed.slice(matched.code.length).replace(/\D/g, "");
|
||||||
|
setPhone(rest.slice(0, matched.maxLength));
|
||||||
|
if (phoneError) setPhoneError("");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if user pasted dial digits without '+' (e.g. 919876543210)
|
||||||
|
const dialDigits = selectedCountry.code.replace(/\D/g, "");
|
||||||
|
const rawDigits = trimmed.replace(/\D/g, "");
|
||||||
|
if (
|
||||||
|
dialDigits &&
|
||||||
|
rawDigits.startsWith(dialDigits) &&
|
||||||
|
rawDigits.length > selectedCountry.maxLength
|
||||||
|
) {
|
||||||
|
const rest = rawDigits.slice(dialDigits.length);
|
||||||
|
setPhone(rest.slice(0, selectedCountry.maxLength));
|
||||||
|
if (phoneError) setPhoneError("");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const digits = text.replace(/\D/g, "");
|
const digits = text.replace(/\D/g, "");
|
||||||
setPhone(digits.slice(0, selectedCountry.maxLength));
|
setPhone(digits.slice(0, selectedCountry.maxLength));
|
||||||
if (phoneError) setPhoneError("");
|
if (phoneError) setPhoneError("");
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,10 @@ import {
|
||||||
getCountryRuleByName,
|
getCountryRuleByName,
|
||||||
updatePhoneDialCode,
|
updatePhoneDialCode,
|
||||||
validatePhoneNumber,
|
validatePhoneNumber,
|
||||||
|
isValidPhoneNumber,
|
||||||
|
parsePhoneNumber,
|
||||||
|
formatE164Phone,
|
||||||
|
cleanPhoneInput,
|
||||||
} from "@/constants/countries";
|
} from "@/constants/countries";
|
||||||
|
|
||||||
interface EditProfileFormProps {
|
interface EditProfileFormProps {
|
||||||
|
|
@ -53,34 +57,6 @@ const COMMON_COUNTRIES = [
|
||||||
"United States",
|
"United States",
|
||||||
];
|
];
|
||||||
|
|
||||||
export const isValidPhoneNumber = (val?: string, country?: string): boolean => {
|
|
||||||
if (!val || !val.trim()) return true;
|
|
||||||
return validatePhoneNumber(val, undefined, country) === null;
|
|
||||||
};
|
|
||||||
|
|
||||||
const cleanPhoneInput = (text: string, maxLength = 16): string => {
|
|
||||||
const trimmedStart = text.trimStart();
|
|
||||||
const hasPlus = trimmedStart.startsWith("+");
|
|
||||||
const rest = hasPlus ? trimmedStart.slice(1) : text;
|
|
||||||
// Disallow all non-digits except a single separating space after country code
|
|
||||||
// Specifically strip hyphens, dashes, parentheses, underscores, and letters
|
|
||||||
const noDashes = rest.replace(/[\-()_a-zA-Z]/g, "");
|
|
||||||
const normalized = noDashes.replace(/\s+/g, " ");
|
|
||||||
let digitCount = 0;
|
|
||||||
let result = "";
|
|
||||||
for (const ch of normalized) {
|
|
||||||
if (/\d/.test(ch)) {
|
|
||||||
if (digitCount < maxLength) {
|
|
||||||
digitCount++;
|
|
||||||
result += ch;
|
|
||||||
}
|
|
||||||
} else if (ch === " " && digitCount > 0 && !result.includes(" ")) {
|
|
||||||
result += ch;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return hasPlus ? `+${result}` : result;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default function EditProfileForm({
|
export default function EditProfileForm({
|
||||||
initialName = "User Name",
|
initialName = "User Name",
|
||||||
initialEmail = "user@example.com",
|
initialEmail = "user@example.com",
|
||||||
|
|
@ -118,10 +94,10 @@ export default function EditProfileForm({
|
||||||
};
|
};
|
||||||
|
|
||||||
const handlePhoneChange = (text: string) => {
|
const handlePhoneChange = (text: string) => {
|
||||||
const cleaned = cleanPhoneInput(text);
|
const cleaned = cleanPhoneInput(text, activeRule.maxLength + activeRule.code.length + 2);
|
||||||
setPhone(cleaned);
|
setPhone(cleaned);
|
||||||
if (phoneError) {
|
if (phoneError) {
|
||||||
if (!cleaned.trim() || isValidPhoneNumber(cleaned, country)) {
|
if (!cleaned.trim() || isValidPhoneNumber(cleaned, activeRule.code, country)) {
|
||||||
setPhoneError("");
|
setPhoneError("");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -164,13 +140,8 @@ export default function EditProfileForm({
|
||||||
let normalizedPhone = "";
|
let normalizedPhone = "";
|
||||||
if (trimmedPhone) {
|
if (trimmedPhone) {
|
||||||
const rule = getCountryRuleByName(country);
|
const rule = getCountryRuleByName(country);
|
||||||
const digitsOnly = trimmedPhone.replace(/\D/g, "");
|
const parsed = parsePhoneNumber(trimmedPhone, country);
|
||||||
const dialDigits = rule.code.replace(/\D/g, "");
|
normalizedPhone = formatE164Phone(rule.code, parsed.localDigits);
|
||||||
let localDigits = digitsOnly;
|
|
||||||
if (digitsOnly.startsWith(dialDigits) && digitsOnly.length > dialDigits.length) {
|
|
||||||
localDigits = digitsOnly.slice(dialDigits.length);
|
|
||||||
}
|
|
||||||
normalizedPhone = `${rule.code}${localDigits}`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onSave?.(trimmedName, normalizedPhone, country);
|
onSave?.(trimmedName, normalizedPhone, country);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue