Using Google’s libphonenumber Library to Parse and Validate Phone Numbers
Techby Sunny Srinidhi -January 9, 20200
We all work with phone numbers phonenumberproto in almost any project or product which has human users. And when the product is available to a global user base, it becomes very difficult to maintain valid phone numbers in the database.
We need to make sure phonenumberproto
Numbers for different regions are of the proper length for their regions, add country codes, or remove them, and a lot of such validations. This could become a project of its own pretty soon.
We had such an issue in one china phone number library of our projects. When I was doing the research to find an easy to use and light weight tool so that I could outsource the smarts involved in this to, I came across the libphonenumber library by Google. Google has everything you need. So without thinking phonenumberproto about it twice (which is bad), I just started looking at the documentation to understand how I can integrate this to my project to make my life easier. In this post, I’ll try and help you understand the same.
As you already know phonenumberproto
I write most of my projects in Spring Boot, this is no different. I’m using a Spring Boot command line runner for this POC. One awesome thing is, the Java client for this Google library is very optimised, such that you can use it in an phonenumberproto Android app as well. It’s that small in size and that quick in response. In fact, all Android versions starting from Android 4.0 (Ice Cream Sandwich), have this library baked into the system. So, let’s get started.
Importing the library
The libphonenumber library phonenumberproto is available in the Maven central repository, so importing the library into your project is as easy as adding a dependency in the pom.xml file. Just add the following dependency in your pom.xml file and you’re done:The Code
Let’s now get into the what image formats to use on your website to optimize performance fun part. In this example, we’re going to take a few Brazilian and a few Colombian phone numbers, and their respective country short codes, and try to figure out the
following about them
Are the phone numbers valid for their regions?
Do they have a country code present? If so, what is it?
Do they have a national phonenumberproto number present? If so, what is it?
Formatting the phone numbers to the E164 standard.
Before we begin with this, let’s look at the phone numbers we have:
As you can see, it’s not 100% accurate, but job data most of the times, it get’s it right. Depending on your application, you could decide if this level of accuracy is enough for you or not. But the library does come in handy when phonenumberproto you’re dealing with phone numbers.
There’s a lot more you could with this library. I encourage you to check out the Github repo of the library (yes, it’s open sourced, of course) and try to figure out yourself if this serves to your needs.
If you’re interested in getting
your hands dirty with a ready project which you can just run, head over to my Github repo where you’ll get the project from phonenumberproto which I took out these code snippets, and you’ll be able to just run it and get the output. And if you found this post or any other of my posts helpful, consider supporting my on Patreon below.