Parse, Validate and Format Phone Numbers in Code

Working examples for PHP, Python, JavaScript, Java and C#, all built on Google's libphonenumber. Enter a number below and every snippet updates with the real output for that number.

Valid GB number. Outputs below come from libphonenumber.

composer require giggsey/libphonenumber-for-php
<?php
use libphonenumber\PhoneNumberUtil;
use libphonenumber\PhoneNumberFormat;
use libphonenumber\NumberParseException;

$util = PhoneNumberUtil::getInstance();

try {
    $number = $util->parse('07400 123456', 'GB');

    $util->isValidNumber($number);                              // true
    $util->format($number, PhoneNumberFormat::E164);            // +447400123456
    $util->format($number, PhoneNumberFormat::INTERNATIONAL);   // +44 7400 123456
    $util->format($number, PhoneNumberFormat::NATIONAL);        // 07400 123456
    $util->format($number, PhoneNumberFormat::RFC3966);         // tel:+44-7400-123456
} catch (NumberParseException $e) {
    // Not a phone number, or no country could be determined
}

The Same Four Steps in Every Language

Every libphonenumber port follows the same model, so once you know it in one language you know it in all of them:

  1. Parse the raw string with a default region, which turns it into a phone number object holding the country code and national number.
  2. Validate it: isValidNumber checks the length and range against the country's plan, and isPossibleNumber checks the length only.
  3. Store the E.164 string. It is unique, sortable and accepted by every SMS and voice API.
  4. Format on output: international for global display, national for local display, and RFC3966 for tel: links.

Libraries by Language

LanguagePackageInstall
PHPgiggsey/libphonenumber-for-phpcomposer require giggsey/libphonenumber-for-php
Pythonphonenumberspip install phonenumbers
JavaScript / TypeScriptlibphonenumber-jsnpm install libphonenumber-js
Java / Kotlincom.googlecode.libphonenumberMaven / Gradle: libphonenumber
C# / .NETlibphonenumber-csharpdotnet add package libphonenumber-csharp

In JavaScript, libphonenumber-js comes in three metadata sizes. /min is enough for parsing, validation and formatting. Import /max when you need getType() to tell mobile from landline in every country. This site's formatter uses /min and the validator uses /max.

Mistakes That Break Phone Number Handling

  • Storing numbers as integers. That loses the + and any leading zero. Use a string column.
  • Validating with a regex only. ^\+[1-9]\d{1,14}$ checks the shape of E.164, not whether the number can exist. Use it as a cheap first check, then use libphonenumber.
  • Parsing without a default region. 07400 123456 means nothing without knowing it is British. Use the user's country from the signup form, the shipping address or the browser locale.
  • Formatting before storing. Store E.164 and format on display. Otherwise (415) 555-0132 and +1 415 555 0132 become two different customers.
  • Letting the metadata go stale. Countries change numbering plans every year, so update the library regularly to keep validation accurate.

Common Questions

How do I format a phone number to international format in PHP?

Install giggsey/libphonenumber-for-php with Composer, parse the number with PhoneNumberUtil::getInstance()->parse($number, 'GB'), then call format($parsed, PhoneNumberFormat::INTERNATIONAL) for +44 7400 123456 or PhoneNumberFormat::E164 for +447400123456. The second argument to parse() is the default country, used when the number has no + country code.

What is the best library for parsing phone numbers?

Google's libphonenumber. It ships metadata for every country's numbering plan and has maintained ports for most languages: libphonenumber-js for JavaScript and TypeScript, phonenumbers for Python, giggsey/libphonenumber-for-php for PHP, libphonenumber-csharp for .NET, and the original Java and C++ versions.

Can I validate phone numbers with a regex?

Only the shape. ^\+[1-9]\d{1,14}$ confirms that a string looks like E.164, but it accepts numbers with the wrong length for their country and prefixes that do not exist. Real validation needs each country's numbering plan, which is what libphonenumber provides.

How should I store phone numbers in a database?

Store the E.164 string (for example +447400123456) in a VARCHAR(16) column: 15 digits maximum plus the plus sign. Keep extensions in a separate column. Format for display when you read the number, not when you write it, and never store numbers as integers, because that drops the + and any leading zeros.

Why does the RFC3966 output differ between libraries?

Both forms are valid tel: URIs. Google's Java, Python, PHP and C# libraries separate the groups with hyphens (tel:+44-7400-123456), while libphonenumber-js returns the compact form (tel:+447400123456). Phones and browsers dial both the same way.

What does the default region argument do?

It tells the parser which country to assume when the input has no + or 00 prefix. '07400 123456' needs a default region of GB to be read correctly. Input that already starts with +44 ignores the default region. Python's phonenumbers raises NumberParseException if a number without a country code has no default region.