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: Handling Global Number Formats
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.
International Phone Validation Overview
Overview {#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 {#e164-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 {#country-rules}
Different countries have different validation rules.
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 {#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 {#validation}
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 {#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.
Validate phone numbers globally with our international phone validation APIs, supporting 200+ countries with accurate, country-specific validation rules.