Check phone number activity, carrier details, line type and more.
E164 Format: Specifications and Phone Number Validation Guide
The E.164 standard, established by the International Telecommunication Union (ITU), serves as the cornerstone for international telephone numbering. It defines the format for telephone numbers used in public telecommunication networks globally, effectively paving the way for seamless communication between users across diverse regions. In a world increasingly reliant on interconnected systems, understanding E.164 specifications and how to validate these numbers is crucial for developers, businesses, and telecom engineers alike.
At its core, the E.164 standard defines the structure of telephone numbers. Each number can comprise a maximum of 15 digits, including a country code, which ranges from one to three digits, followed by the national significant number (NSN). The NSN further breaks down into the national destination code (NDC), which identifies the specific region or operator, and the subscriber number (the individual account within the NDC). For example, in the number +14155552671, +1 is the country code for the United States, while 4155552671 is the national significant number.
E.164 Specifications
The E.164 standard enforces the numerical format for phone numbers, prohibiting any letters, symbols, or other characters. This structured approach ensures a consistent and simple format across different telecommunication networks. The specifics of E.164 include:
Maximum Length: An E.164 number is limited to 15 digits, which aids in keeping the format manageable across different systems.
Global Format: E.164 numbers must begin with a + sign followed by the country code and subscriber number, paving the way for easy international dialing.
No Local Dialing Prefix: The specification omits any local dialing prefixes unique to specific countries, ensuring universal recognition.
Validating E.164 Numbers
Validation of E.164 numbers is a crucial process in ensuring data accuracy and integrity, especially in user input fields for applications handling global communications. Validation involves checking that the number adheres to the E.164 specifications using several factors:
Checking the length to ensure it does not exceed 15 digits.
Verifying that the number starts with a + sign, followed by the country code and other digits.
Utilizing libraries or built-in functions that can parse and validate phone numbers based on the E.164 format, such as libphonenumber or intl-tel-input.
Ensuring the country code is accurate and corresponds to existing codes recognized within the E.164 standard.
Developers can implement this validation in their applications using simple regex patterns or pre-built libraries. For example, a basic regex to validate an E.164 number might look like this:
/^\+?[1-9]\d{1,14}$/
This regex checks for the optional + sign, followed by a permissible country code and the remaining digits.
Using the Phone Library for Number Formatting and Validation
The phone library is a powerful tool designed to normalize mobile phone numbers into the E.164 format. Many users enter phone numbers in various formats, such as (817) 569-8900, 817569-8900, or even +1(817) 569-8900. The library ensures that these inputs are standardized into a uniform format: +18175698900.
Installation
To get started, you can easily install the package using npm or yarn:
npm install phone
yarn add phone
Basic Usage
Once installed, you can import and use the library as follows:
const { phone } = require('phone');
// or
import { phone } from 'phone';
Example: Simple Validation
To validate and format a phone number, simply call the phone function with the desired number:
* phoneNumber (string): The phone number you want to process (required).
* country (string): Optional ISO-3166 alpha 2 or 3 country code.
* validateMobilePrefix (boolean): Set to false to skip mobile prefix validation.
* strictDetection (boolean): Set to true to disable trunk code detection.
Return Type:
The function returns an object indicating whether the phone number is valid, along with the formatted version and relevant country codes.
FAQ
Does the phone library perform logical validation?
Yes, it validates the structure of the provided phone number against known formats. However, if you're validating landline numbers, you must set validateMobilePrefix to false to allow for non-mobile inputs.
What if my valid phone number returns an invalid result?
The function defaults to validating mobile numbers. If you need to include landlines, you can adjust the settings as outlined above.
How do I contribute?
If you notice incorrect phone number formats or wish to propose enhancements, feel free to create a pull request with references for verification.
By using the phone library, developers can ensure better data integrity when dealing with user inputs for phone numbers, making applications worldwide compliant with a standard formatting convention.
Using the libphonenumber-js Library for Phone Number Parsing and Validation
libphonenumber-js is a simpler and smaller rewrite of Google's libphonenumber library specifically for JavaScript. With a focus on reducing size and complexity, this library provides essential features for phone number parsing and formatting, making it ideal for modern web applications.
Installation
To add libphonenumber-js to your project, you can install it via npm or yarn:
npm install libphonenumber-js --save
yarn add libphonenumber-js
Basic Usage
Here’s how to use the library for phone number parsing:
Compared to Google's Java and C++ implementation, libphonenumber-js has a significantly smaller footprint (145kB vs. 550kB), making it a more efficient choice for web applications.
2. Parsing and Formatting
The library can parse various formats of phone numbers while focusing on personal, internationally-dialable numbers. It does not support formatting for non-conventional numbers such as emergency numbers or special marketing codes.
The library includes an "As You Type" formatter that formats numbers dynamically as the user types:
import { AsYouType } from 'libphonenumber-js';
const formatter = new AsYouType('US');
console.log(formatter.input('2133734')); // Outputs: '(213) 373-4'
6. Full-text Search
This feature allows you to find phone numbers within a block of text:
import { findPhoneNumbersInText } from 'libphonenumber-js';
const foundNumbers = findPhoneNumbersInText(`For tech support call +7 (800) 555-35-35 or reach a local US branch at (213) 373-4253 ext. 1234.`, 'US');
// Outputs the two found phone numbers along with their metadata.
API Overview
The libphonenumber-js library offers a comprehensive API:
* parsePhoneNumber(string, defaultCountry?): PhoneNumber - Parses a phone number from a string.
* isPossiblePhoneNumber(string, defaultCountry?): boolean - Checks if the input can be parsed as a possible phone number.
* isValidPhoneNumber(string, defaultCountry?): boolean - Validates the input as a valid phone number.
* public getExampleNumber(country: string): PhoneNumber - Retrieves an example phone number for a specified country.
* findPhoneNumbersInText(text: string, defaultCountry?): object[] - Searches for phone numbers within a given text.
Metadata Options
This library provides various metadata sets that can be used for more accurate validation:
* max - The complete metadata set (145kB).
* min - The smallest metadata set (80kB), which is the default.
* mobile - Metadata specific to mobile numbers (95kB).
Contributing
If you’re interested in contributing to the library, ensure that you've installed the necessary dependencies:
npm install
After making your changes, run tests to ensure code coverage remains at 100%:
npm test
Resources
For those developing with React, consider checking out react-phone-number-input for a component built on this library.
With libphonenumber-js, developers can efficiently handle phone number parsing and validation, ensuring that user-input data meets international standards without the overhead of larger libraries.
Using the google-libphonenumber Library for Phone Number Parsing and Validation
google-libphonenumber is an up-to-date and reliable implementation of Google’s libphonenumber for Node.js, providing essential features to parse, format, store, and validate international phone numbers.
Installation
To install the package, simply run the following command:
npm install --save-prod google-libphonenumber
Basic Usage
Here’s a quick example of how to extract phone information:
const PhoneNumberUtil = require('google-libphonenumber').PhoneNumberUtil;
const phoneUtil = PhoneNumberUtil.getInstance();
// Parse the phone number
const number = phoneUtil.parseAndKeepRawInput('202-456-1414', 'US');
// Check validity and format
console.log(phoneUtil.isValidNumberForRegion(number, 'US')); // => true
console.log(phoneUtil.format(number, PNF.E164)); // => +12024561414
console.log(phoneUtil.formatInOriginalFormat(number, 'US')); // => (202) 456-1414
Important Notes
Most functions in libphonenumber expect an instance of libphonenumber.PhoneNumber, which you can obtain by parsing a raw string number. For example:
phoneUtil.isValidNumberForRegion(phoneUtil.parse('202-456-1414', 'US'), 'US'); // This works
phoneUtil.isValidNumberForRegion('202-456-1414', 'US'); // This will not work!
The "As You Type" Formatter
The "As You Type" formatter shows the formatting progress as a number is being typed. Each keystroke must be registered as shown below:
Here's a glimpse of the core API with essential methods:
* format(number, numberFormat) - Formats a phone number in the specified format.
* isPossibleNumber(number) - Checks if the number is a possible fully-qualified number.
* isValidNumber(number) - Determines whether a number matches a valid pattern.
* parseAndKeepRawInput(numberToParse, defaultRegion) - Parses a string and returns it in proto buffer format while keeping the raw input value.
* getRegionCodeForNumber(number) - Returns the region where a phone number is from.
Tracking Short Numbers
The library also provides tools for working with short numbers, including:
The google-libphonenumber package maintains a direct 1:1 mapping to the original library, which means it does not implement significant optimizations or simplifications like some alternatives. It is based on the latest version of the Google Closure library.
Metadata Considerations
If you encounter validation issues or formatting errors, first check the official demo page for responses. If discrepancies exist, it could indicate a metadata update is needed for this package.
Notes on Usage with Webpack
Some users have reported issues with Webpack. If you encounter problems, modifications might be necessary to improve compatibility. Contributions to this are welcome!
Testing and Reporting
The package includes a small set of tests to verify function exports. For a comprehensive test suite, leverage the original libphonenumber’s extensive testing.
npm test
Conclusion
With google-libphonenumber, developers can effectively manage phone number parsing and validation within their Node.js applications, leveraging a fully-featured library built upon Google's well-established metadata.
Using the Intl-Tel-Input Library for International Telephone Input
intl-tel-input is a JavaScript plugin that enhances your input fields for entering and validating international telephone numbers. With features like auto-detection of the user's country and formatting as you type, it's an essential tool for ensuring accurate phone number entries in web applications.
New Features
Recent updates include:
* Vue and React components for seamless integration.
* Translations available in over 30 languages.
Installation
To install intl-tel-input, you can use npm or yarn:
npm install intl-tel-input --save
yarn add intl-tel-input
Getting Started
To use the plugin via CDN, include the CSS and JavaScript files in your HTML:
* Automatically detects the user's country and adjusts the placeholder accordingly.
* Formats the number as the user types.
* Comprehensive validation methods to ensure correct input.
* Accessibility support via ARIA tags.
* Customizable styles with CSS variables.
* Translations for better localization.
Initialization Options
When initializing the plugin, you can customize it using various options. Some notable options include:
* allowDropdown: Enable or disable the dropdown.
* autoPlaceholder: Set how placeholders behave when the country changes.
* geoIpLookup: Provide a function to determine the user's country based on IP.
* initialCountry: Specify the initial country code, or set to "auto" for auto-detection.
* hiddenInput: Create hidden fields to store the full international telephone number.
Instance Methods
After initializing the plugin, you can interact with it using methods like:
* getNumber(): Get the current number in specified formats.
* isValidNumber(): Check if the current number is valid.
* setCountry(code): Change the selected country programmatically.
Events
The plugin supports various events such as:
* countrychange: Triggered when the user selects a country.
* open:countrydropdown: When the dropdown is opened.
* close:countrydropdown: When the dropdown is closed.
Theming / Dark Mode
You can easily customize the appearance of the plugin using CSS variables. For dark mode, override the default styles with your own preference:
If you encounter issues such as the dropdown not closing on scroll or full-width inputs, check the debugging suggestions provided in the full documentation.
Contributing
For contributions, please refer to the contributing guide provided within the repository for instructions on updates or improvements.
Conclusion
With intl-tel-input, adding robust international telephone input has never been easier, ensuring your application captures accurate phone numbers for a global audience.