Do you want your ad here?

Contact us to get your ad seen by thousands of users every day!

[email protected]

The Best Way to Handle Phone Numbers

  • July 05, 2024
  • 8386 Unique Views
  • 2 min read
Table of Contents
Introduction Google's libphonenumberCreating and Formatting Phone NumbersParsing Phone NumbersValidating Phone NumbersConclusion and Recommendation

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);

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 has even more features, like finding phone numbers in text, getting the number's type, and so on. In my opinion, it's the go-to library for processing phone numbers.

Finally, to answer the question of 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.

Foojay Podcast #36: J-Fall Report, Part 3

In this part, hear about Maven, contributing to Open Source projects, JOOQ, Desktop Applications, Thymeleaf, htmx, and Security.

Foojay Podcast #46: JUG Switzerland

In this episode, Frank and the Foojay Podcast leave the European Union and step over the border of Switzerland, the country where the Red Cross was started, and many international institutions have their headquarters.

Foojay Podcast #53: JCON Report, Part 5 – CQRS, JOOQ, GraphQL, API, Vaadin, OpenRewrite, ErrorProne, Gateways, Proxies,…

In this episode you’ll hear Simon Martinelli, Nicolas Fränkel, Marcus Hellberg, Rick Ossendrijver, and Abdel Sghiouar.

Looking Back on One Year of Speaking and Blogging

2023 was an adventurous year for me: I came into my blogging rhythm, blogging every one to two weeks, resulting in 39 articles, many of them on Foojay.io.

Master-Detail with Hilla

Learn how to use the web application framework Hilla to create a master-detail view with a Grid to display the data and a Form to edit the data.

Do you want your ad here?

Contact us to get your ad seen by thousands of users every day!

[email protected]

Comments (0)

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

No comments yet. Be the first.

Subscribe to foojay updates:

https://foojay.io/feed/
Copied to the clipboard