International Phone Validation: Handling Global Number Formats
Master the complexities of validating phone numbers across different countries, formats, and numbering systems.
Table of Contents
Table of Contents
International phone validation requires understanding diverse numbering plans, cultural conventions, and regulatory requirements. Building globally compatible validation systems demands comprehensive knowledge of regional variations and standards.
Cleariflow Phone Validation API scope: Our Phone Validation API performs structural validation via Google libphonenumber, E.164 normalization, line type, and optional carrier/location metadata (mainly US/CA). It does not perform HLR/MNP lookups, line-status checks, or subscriber verification. Advanced topics below describe patterns you can combine with the API in a larger stack.
Overview
Phone number formats vary dramatically by country, requiring country-specific validation rules. ITU-T E.164 provides the global framework, but implementation requires handling regional numbering plans, formatting conventions, and carrier-specific patterns.
E.164 Standard
International standard for phone numbering.
interface E164Number {
countryCode: string // 1-3 digits
nationalNumber: string // Up to 15 total digits
formatted: string
}
function parseE164(number: string): E164Number {
const cleaned = number.replace(/D/g, '')
const countryCode = cleaned.slice(0, 1) // Simplified
const nationalNumber = cleaned.slice(1)
return {
countryCode,
nationalNumber,
formatted: `+${countryCode} ${nationalNumber}`
}
}
Country-Specific Rules
Different countries have different validation rules. In production, prefer libphonenumber (as used by Cleariflow) over hand-maintained regex tables — numbering plans change frequently.
interface CountryRules {
code: string
minLength: number
maxLength: number
pattern: RegExp
format: string
}
const COUNTRY_RULES: Record<string, CountryRules> = {
'US': {
code: '+1',
minLength: 10,
maxLength: 10,
pattern: /^[2-9]d{2}[2-9]d{6}$/,
format: '(XXX) XXX-XXXX'
},
'UK': {
code: '+44',
minLength: 10,
maxLength: 10,
pattern: /^[1-9]d{9}$/,
format: 'XXXX XXX XXXX'
},
'DE': {
code: '+49',
minLength: 10,
maxLength: 11,
pattern: /^[1-9]d{9,10}$/,
format: 'XXX XXXXXXXX'
}
}
Number Formatting
Format numbers according to local conventions.
class PhoneFormatter {
format(number: string, country: string): string {
const rules = COUNTRY_RULES[country]
if (!rules) return number
const cleaned = number.replace(/D/g, '')
let formatted = rules.code + ' '
// Apply country-specific formatting
// Simplified implementation
return formatted + cleaned
}
}
Validation Implementation
Validate phone numbers globally.
class InternationalPhoneValidator {
validate(number: string, expectedCountry?: string): {
valid: boolean
country: string
formatted: string
} {
const cleaned = number.replace(/D/g, '')
for (const [country, rules] of Object.entries(COUNTRY_RULES)) {
if (expectedCountry && country !== expectedCountry) continue
const nationalNumber = cleaned.replace(rules.code.replace('+', ''), '')
if (nationalNumber.length >= rules.minLength &&
nationalNumber.length <= rules.maxLength &&
rules.pattern.test(nationalNumber)) {
return {
valid: true,
country,
formatted: rules.code + ' ' + nationalNumber
}
}
}
return { valid: false, country: 'unknown', formatted: number }
}
}
Conclusion
International phone validation requires understanding E.164 standard, implementing country-specific rules, handling diverse formatting conventions, and providing appropriate user feedback. Success depends on maintaining comprehensive country rules, supporting multiple input formats, and providing clear formatting guidance.
Use the Cleariflow Phone Validation API for structural checks and E.164 normalization across 240+ regions — with an optional country hint for national-format input.