fix(validation): add country-specific mobile phone validation and libphonenumber-js #110

Merged
amit merged 3 commits from fix/country-specific-phone-validation into main 2026-11-19 05:11:22 +00:00
1 changed files with 60 additions and 57 deletions
Showing only changes of commit 1dd80f23ba - Show all commits

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';
import { import {
COUNTRY_CODES, COUNTRY_CODES,
getCountryRuleByDialCode, getCountryRuleByDialCode,
@ -64,74 +64,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+/, '')
: nationalDigits;
if (parsed && parsed.isValid()) { // 1. Enforce length validation FIRST so digit count errors take precedence over prefix errors
const numberType = parsed.getType();
// Specific mobile-first checks for countries with distinct mobile ranges
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') {
// In France, mobile numbers start with 6 or 7 (national 06 or 07) if (nationalDigits.length < 9) {
if (numberType === 'FIXED_LINE' || !/^[67]/.test(parsed.nationalNumber)) { 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') {
// In UK, mobile numbers start with 7 (national 07) if (digitsWithoutTrunk0.length < 10) {
if (numberType === 'FIXED_LINE' || !/^7/.test(parsed.nationalNumber)) { 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 {
// Disallow pure fixed-line/landline numbers when mobile numbers are expected // 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;
} }
// Detailed length validation error fallback
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.`;
}
// Format or prefix error
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}).`;
}; };