The Best Way to Handle Phone Numbers
![blog-post-img](https://martinelli.ch/wp-content/uploads/2024/06/pexels-pixabay-163007-2.jpg)
Processing phone numbers seems complicated at first glance because of the many different formats. In this post, I’ll show you that with libphonenumber, it becomes child’s play. I’ll also recommend how to store the phone number in the database.
Introduction Google’s libphonenumber
According to the GitHub repository https://github.com/google/libphonenumber/ libphone number is:
Google’s common Java, C++ and JavaScript library for parsing, formatting, and validating international phone numbers. The Java version is optimized for running on smartphones, and is used by the Android framework since 4.0 (Ice Cream Sandwich).
Let’s see the library in action. The source code is available here: https://github.com/simasch/libphonenumber-demo
Creating and Formatting Phone Numbers
The main class of libphonenumber is, no surprise, the class Phonenumber
. You’ll want to use a phone number in two ways to create a phone number.
First, from country code and national number:
Phonenumber.PhoneNumber swissPhoneNumber = new Phonenumber.PhoneNumber(); swissPhoneNumber.setCountryCode(41); swissPhoneNumber.setNationalNumber(324556677L);
We can now format the phone number in different ways:
PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance(); System.out.println(phoneNumberUtil.format(swissPhoneNumber, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL)); System.out.println(phoneNumberUtil.format(swissPhoneNumber, PhoneNumberUtil.PhoneNumberFormat.NATIONAL));
This will print:
+41 32 455 66 77 032 455 66 77
Parsing Phone Numbers
But what if we get the phone number as a string? No worries, the PhoneNumberUtil
cannot only format but also parse phone numbers:
Phonenumber.PhoneNumber parsedSwissPhoneNumber = phoneNumberUtil.parse("032 455 66 77", "CH");
The first argument is the phone number as a string, and the second parameter is the region we expect the number to be from; this is only used if the number string is not an international number.
Validating Phone Numbers
Now that we can parse strings to phone numbers, how can we check if the phone number is valid?
Also, here, PhoneNumberUtil, has you covered it as it has metadata of the phone numbers of many regions?
Phonenumber.PhoneNumber invalidSwissPhoneNumber = phoneNumberUtil.parse("032 631 11 2", "CH"); System.out.println(phoneNumberUtil.isValidNumber(invalidSwissPhoneNumber));
The code above will print false as the phone number is too short.
Conclusion and Recommendation
Google’s libphonnumber is very powerful, and it has even more features, like finding phone numbers in text, getting the type of the number, and so on. In my opinion, it’s the go-to library when it comes to processing phone numbers.
Finally, to answer how a phone number should be stored in the database, I recommend storing the country code and national number separately as numbers. That way, you can directly create a libphonenumber Phonenumber
from the data and format it when needed.