fix(validation): prioritize length errors over prefix errors in phone validation

This commit is contained in:
Urvish Prajapati 2026-11-16 16:30:50 +05:30
parent 0c60b835a0
commit b2fe1e635e
1 changed files with 60 additions and 51 deletions

View File

@ -1,4 +1,4 @@
import { parsePhoneNumberFromString, validatePhoneNumberLength, type CountryCode as LibCountryCode } from "libphonenumber-js/max";
import { parsePhoneNumberFromString, type CountryCode as LibCountryCode } from "libphonenumber-js/max";
export interface CountryCode {
iso2: LibCountryCode;
@ -197,68 +197,77 @@ export const validatePhoneNumber = (
const iso2 = rule.iso2 || "IN";
// Format candidate string for libphonenumber-js
let fullToParse = trimmed;
if (!fullToParse.startsWith("+")) {
const dialDigits = rule.code.replace(/\D/g, "");
if (allDigits.startsWith(dialDigits) && allDigits.length > rule.maxLength) {
fullToParse = `+${allDigits}`;
// Extract national digits without dial code prefix
let nationalDigits = allDigits;
const dialDigits = rule.code.replace(/\D/g, "");
if (trimmed.startsWith("+")) {
if (allDigits.startsWith(dialDigits)) {
nationalDigits = allDigits.slice(dialDigits.length);
}
} else if (allDigits.startsWith(dialDigits) && allDigits.length > rule.maxLength) {
nationalDigits = allDigits.slice(dialDigits.length);
}
const digitsWithoutTrunk0 = nationalDigits.startsWith("0") && iso2 !== "IT"
? nationalDigits.replace(/^0+/, "")
: nationalDigits;
// 1. Enforce length validation FIRST so digit count errors take precedence over prefix errors
if (iso2 === "IN") {
if (nationalDigits.length !== 10) {
return "India mobile number must be exactly 10 digits.";
}
if (!/^[6-9]/.test(nationalDigits)) {
return "India mobile number must begin with 6, 7, 8, or 9.";
}
} else if (iso2 === "FR") {
if (nationalDigits.length < 9) {
return "France mobile number is too short (must be 9 digits, or 10 digits with 0).";
}
if (nationalDigits.length > 10 || (nationalDigits.length === 10 && !nationalDigits.startsWith("0"))) {
return "France mobile number cannot exceed 9 digits (or 10 digits with leading 0).";
}
if (!/^[67]/.test(digitsWithoutTrunk0)) {
return "France mobile number must begin with 06 or 07 (or 6 or 7).";
}
} else if (iso2 === "GB") {
if (digitsWithoutTrunk0.length < 10) {
return "United Kingdom mobile number is too short (must be 10 digits, or 11 digits with 0).";
}
if (digitsWithoutTrunk0.length > 10) {
return "United Kingdom mobile number cannot exceed 10 digits (or 11 digits with leading 0).";
}
if (!/^7/.test(digitsWithoutTrunk0)) {
return "United Kingdom mobile number must begin with 07 (or 7).";
}
} else {
// General length check for other countries
if (rule.minLength === rule.maxLength) {
if (digitsWithoutTrunk0.length !== rule.minLength && nationalDigits.length !== rule.minLength) {
return `${rule.country} mobile number must be exactly ${rule.minLength} digits.`;
}
} else {
fullToParse = `${rule.code}${allDigits}`;
if (digitsWithoutTrunk0.length < rule.minLength && nationalDigits.length < rule.minLength) {
return `${rule.country} mobile number is too short (min ${rule.minLength} digits).`;
}
if (digitsWithoutTrunk0.length > rule.maxLength) {
return `${rule.country} mobile number cannot exceed ${rule.maxLength} digits.`;
}
}
}
const parsed = parsePhoneNumberFromString(fullToParse, iso2);
// 2. Validate telecom numbering rules with libphonenumber-js
const candidate = trimmed.startsWith("+") ? trimmed : `${rule.code}${nationalDigits}`;
const parsed = parsePhoneNumberFromString(candidate, iso2);
if (parsed && parsed.isValid()) {
const numberType = parsed.getType();
if (iso2 === "IN") {
if (!/^[6-9]/.test(parsed.nationalNumber)) {
return "India mobile number must begin with 6, 7, 8, or 9.";
}
} else if (iso2 === "FR") {
if (numberType === "FIXED_LINE" || !/^[67]/.test(parsed.nationalNumber)) {
return "France mobile number must begin with 06 or 07 (or 6 or 7).";
}
} else if (iso2 === "GB") {
if (numberType === "FIXED_LINE" || !/^7/.test(parsed.nationalNumber)) {
return "United Kingdom mobile number must begin with 07 (or 7).";
}
} else if (numberType === "FIXED_LINE") {
if (numberType === "FIXED_LINE") {
return `Please enter a valid mobile number for ${rule.country}.`;
}
return null;
}
const nationalDigitsOnly = allDigits.startsWith(rule.code.replace(/\D/g, ""))
? allDigits.slice(rule.code.replace(/\D/g, "").length)
: allDigits;
const lenStatus = validatePhoneNumberLength(nationalDigitsOnly, iso2);
if (lenStatus === "TOO_SHORT") {
return `${rule.country} phone number is too short (min ${rule.minLength} digits).`;
}
if (lenStatus === "TOO_LONG") {
return `${rule.country} phone number cannot exceed ${rule.maxLength} digits.`;
}
if (lenStatus === "INVALID_LENGTH") {
return rule.minLength === rule.maxLength
? `${rule.country} phone number must be exactly ${rule.minLength} digits.`
: `${rule.country} phone number must be between ${rule.minLength} and ${rule.maxLength} digits.`;
}
if (iso2 === "FR") {
return "France mobile number must begin with 06 or 07 (or 6 or 7).";
}
if (iso2 === "IN") {
return "India mobile number must begin with 6, 7, 8, or 9.";
}
if (iso2 === "GB") {
return "United Kingdom mobile number must begin with 07 (or 7).";
}
return `Please enter a valid mobile number for ${rule.country} (e.g. ${rule.placeholder}).`;
};