Guide

Phone Country Codes: Validation and Formatting

Master ITU-T E.164 standard for phone number handling. Learn number validation, country-specific formatting, and SMS/voice API integration with services like Twilio and SMS.ru.

Pavel Volkov
Aug 28, 2025
2 min read
# Phone Country Codes: Complete Developer Guide for Telecommunications Phone country codes are essential for international telecommunications and user verification systems. This comprehensive guide covers everything developers need to know about implementing phone number validation and processing in modern applications. ## Understanding International Dialing System Phone country codes follow the ITU-T E.164 international standard: - **Country Code**: 1-3 digits (e.g., +1 for US/Canada, +44 for UK) - **National Number**: Up to 15 digits total including country code - **Format**: +[Country Code] [National Number] ## Implementing Phone Number Validation ### PHP Validation Library ```php use libphonenumber\PhoneNumberUtil; use libphonenumber\PhoneNumberFormat; class PhoneNumberValidator { private PhoneNumberUtil $phoneUtil; private array $supportedRegions; public function __construct() { $this->phoneUtil = PhoneNumberUtil::getInstance(); $this->supportedRegions = $this->phoneUtil->getSupportedRegions(); } public function validateAndFormat(string $phoneNumber, string $region = null): PhoneValidationResult { try { $parsedNumber = $this->phoneUtil->parse($phoneNumber, $region); $isValid = $this->phoneUtil->isValidNumber($parsedNumber); $isPossible = $this->phoneUtil->isPossibleNumber($parsedNumber); return new PhoneValidationResult([ "is_valid" => $isValid, "is_possible" => $isPossible, "country_code" => $parsedNumber->getCountryCode(), "national_number" => $parsedNumber->getNationalNumber(), "region_code" => $this->phoneUtil->getRegionCodeForNumber($parsedNumber), "number_type" => $this->phoneUtil->getNumberType($parsedNumber), "formatted" => [ "international" => $this->phoneUtil->format($parsedNumber, PhoneNumberFormat::INTERNATIONAL), "national" => $this->phoneUtil->format($parsedNumber, PhoneNumberFormat::NATIONAL), "e164" => $this->phoneUtil->format($parsedNumber, PhoneNumberFormat::E164), "rfc3966" => $this->phoneUtil->format($parsedNumber, PhoneNumberFormat::RFC3966) ] ]); } catch (Exception $e) { return new PhoneValidationResult([ "is_valid" => false, "error" => $e->getMessage() ]); } } } ``` This comprehensive guide provides the foundation for implementing phone country code functionality in modern applications, covering validation, SMS verification, carrier integration, and testing aspects essential for telecommunications features.
Last updated: Sep 04, 2025