fix(validation): prioritize length errors over prefix errors in phone validation
This commit is contained in:
parent
0c60b835a0
commit
b2fe1e635e
|
|
@ -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 {
|
export interface CountryCode {
|
||||||
iso2: LibCountryCode;
|
iso2: LibCountryCode;
|
||||||
|
|
@ -197,68 +197,77 @@ export const validatePhoneNumber = (
|
||||||
|
|
||||||
const iso2 = rule.iso2 || "IN";
|
const iso2 = rule.iso2 || "IN";
|
||||||
|
|
||||||
// Format candidate string for libphonenumber-js
|
// Extract national digits without dial code prefix
|
||||||
let fullToParse = trimmed;
|
let nationalDigits = allDigits;
|
||||||
if (!fullToParse.startsWith("+")) {
|
|
||||||
const dialDigits = rule.code.replace(/\D/g, "");
|
const dialDigits = rule.code.replace(/\D/g, "");
|
||||||
if (allDigits.startsWith(dialDigits) && allDigits.length > rule.maxLength) {
|
if (trimmed.startsWith("+")) {
|
||||||
fullToParse = `+${allDigits}`;
|
if (allDigits.startsWith(dialDigits)) {
|
||||||
} else {
|
nationalDigits = allDigits.slice(dialDigits.length);
|
||||||
fullToParse = `${rule.code}${allDigits}`;
|
|
||||||
}
|
}
|
||||||
|
} else if (allDigits.startsWith(dialDigits) && allDigits.length > rule.maxLength) {
|
||||||
|
nationalDigits = allDigits.slice(dialDigits.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
const parsed = parsePhoneNumberFromString(fullToParse, iso2);
|
const digitsWithoutTrunk0 = nationalDigits.startsWith("0") && iso2 !== "IT"
|
||||||
|
? nationalDigits.replace(/^0+/, "")
|
||||||
if (parsed && parsed.isValid()) {
|
: nationalDigits;
|
||||||
const numberType = parsed.getType();
|
|
||||||
|
|
||||||
|
// 1. Enforce length validation FIRST so digit count errors take precedence over prefix errors
|
||||||
if (iso2 === "IN") {
|
if (iso2 === "IN") {
|
||||||
if (!/^[6-9]/.test(parsed.nationalNumber)) {
|
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.";
|
return "India mobile number must begin with 6, 7, 8, or 9.";
|
||||||
}
|
}
|
||||||
} else if (iso2 === "FR") {
|
} else if (iso2 === "FR") {
|
||||||
if (numberType === "FIXED_LINE" || !/^[67]/.test(parsed.nationalNumber)) {
|
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).";
|
return "France mobile number must begin with 06 or 07 (or 6 or 7).";
|
||||||
}
|
}
|
||||||
} else if (iso2 === "GB") {
|
} else if (iso2 === "GB") {
|
||||||
if (numberType === "FIXED_LINE" || !/^7/.test(parsed.nationalNumber)) {
|
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).";
|
return "United Kingdom mobile number must begin with 07 (or 7).";
|
||||||
}
|
}
|
||||||
} else if (numberType === "FIXED_LINE") {
|
} 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 {
|
||||||
|
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.`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 (numberType === "FIXED_LINE") {
|
||||||
return `Please enter a valid mobile number for ${rule.country}.`;
|
return `Please enter a valid mobile number for ${rule.country}.`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
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}).`;
|
return `Please enter a valid mobile number for ${rule.country} (e.g. ${rule.placeholder}).`;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue