Real-World Validation PatternsLesson 5.3
Phone number validation regex for multiple formats
US phone formats, international format, optional parts, character class for separators, normalization after match, test suite approach
Phone Numbers Come in Too Many Formats
Validate the structure, then normalize to a canonical form. Do not try to match all formats in one rigid pattern.
const phoneRe = /^\(?\d{3}\)?[\s.\-]?\d{3}[\s.\-]?\d{4}$/;
phoneRe.test('4155550000') // true
phoneRe.test('415-555-0000') // true
function normalizePhone(input) {
if (!phoneRe.test(input)) return null;
return input.replace(/\D/g, '');
}
