Phone numbers are a critical component of many applications, and properly parsing, formatting, and validating them can be a challenging task. Fortunately, Google’s libphonenumber library provides a comprehensive solution for handling international phone numbers in Java, C++, and JavaScript.
Key Features
The libphonenumber library offers a wide range of features to handle phone numbers effectively. Here are some of the highlights:
Parsing, Formatting, and Validating
The library allows you to parse, format, and validate phone numbers for all countries/regions of the world. With just a few lines of code, you can convert a phone number string into a PhoneNumber
object, which contains the country code and national number.
java
String phoneNumberString = "+14158888888";
PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
try {
PhoneNumber phoneNumber = phoneNumberUtil.parse(phoneNumberString, "US");
} catch (NumberParseException e) {
System.err.println("NumberParseException was thrown: " + e.toString());
}
The PhoneNumber
object can then be used to perform various operations such as determining the type of the number or checking its validity.
Number Type and Matching
With getNumberType
, you can easily determine the type of a phone number based on the number itself. The library can distinguish Fixed-line, Mobile, Toll-free, Premium Rate, and many other types of phone numbers.
java
PhoneNumber phoneNumber = ... // Obtained from parsing
PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
PhoneNumberType numberType = phoneNumberUtil.getNumberType(phoneNumber);
The library also provides the isNumberMatch
method, which gives you a confidence level on whether two phone numbers could be the same.
Example Numbers
The library includes the getExampleNumber
and getExampleNumberForType
methods, which provide valid example numbers for all countries/regions. You can specify the type of the example phone number needed, such as Mobile or Fixed-line.
java
PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
PhoneNumber exampleNumber = phoneNumberUtil.getExampleNumber("US");
Possible Number Validation
In some cases, you may want to quickly guess whether a number is a possible phone number without performing a full validation. The isPossibleNumber
method allows you to do just that by using only the length information.
java
PhoneNumber phoneNumber = ... // Obtained from parsing
PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
boolean isPossible = phoneNumberUtil.isPossibleNumber(phoneNumber);
Formatting Phone Numbers ‘as you type’
The library provides an AsYouTypeFormatter
class, which allows you to format phone numbers on-the-fly as users enter each digit. This can create a seamless user experience when collecting phone numbers.
java
PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
AsYouTypeFormatter formatter = phoneNumberUtil.getAsYouTypeFormatter("US");
String formattedNumber = formatter.inputDigit('6'); // Outputs "6"
Geocoding and Carrier Information
The library also includes features for geocoding phone numbers and mapping them to original carriers. With the PhoneNumberOfflineGeocoder
class, you can retrieve geographical information related to a phone number.
java
PhoneNumber phoneNumber = ... // Obtained from parsing
PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance();
String location = geocoder.getDescriptionForNumber(phoneNumber, Locale.ENGLISH);
Similarly, the PhoneNumberToCarrierMapper
class allows you to retrieve the original carrier information for a phone number.
java
PhoneNumber phoneNumber = ... // Obtained from parsing
PhoneNumberToCarrierMapper carrierMapper = PhoneNumberToCarrierMapper.getInstance();
String carrierName = carrierMapper.getNameForNumber(phoneNumber, Locale.ENGLISH);
Getting Started and Resources
To get started with libphonenumber, you can include the Java or JavaScript code in your application. For Java, you can integrate with Maven or download the JARs directly from the Maven repository. For JavaScript, you can simply include the library using a script tag or via npm.
Documentation, including Javadoc for the Java version, is available to help you navigate the library’s functionality and use cases.
Conclusion
Google’s libphonenumber library provides a robust solution for parsing, formatting, and validating phone numbers in Java, C++, and JavaScript. Its comprehensive feature set and ease of use make it a valuable tool for any application that handles international phone numbers. Whether you’re building a mobile app, a web application, or an enterprise system, libphonenumber can greatly enhance your phone number handling capabilities.
For more information and code examples, refer to the libphonenumber GitHub repository and the respective documentation for your chosen programming language.
We hope this article has shed light on the extensive capabilities of libphonenumber and how it can benefit your application. If you have any questions or would like to share your experiences using libphonenumber, please don’t hesitate to reach out.
References
- libphonenumber GitHub repository
- Java demo
- JavaScript demo
- libphonenumber third-party ports and alternatives
(author: Blake Bradford)
Leave a Reply