You are here
Home > Tech >

Using Google’s libphonenumber Library to Parse and Validate Phone Numbers

We all work with phone numbers 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 the phone 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 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 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, 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 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 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:

<dependency>
    <groupId>com.googlecode.libphonenumber</groupId>
    <artifactId>libphonenumber</artifactId>
    <version>8.10.14</version>
</dependency>

The Code

Let’s now get into the 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 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:

List<String> brazilPhoneNumbers = Arrays.asList(
        "+5521976923932",
        "5561991474841",
        "055 719 9174 8722",
        "719 9174 8722",
        "55 (719) (9174) (8722)"
);

List<String> columbiaPhoneNumbers = Arrays.asList(
        "573146339375",
        "3137145988",
        "+573154417054",
        "316537219731323123",
        "3013755140",
        "+57 31 5441 7054",
        "+57 (31) (8393) (1081)"
);

As you can see, we have phone numbers in all possible formats for both countries, including some obvious invalid ones. It’ll be fun to see what the Google library thinks about that. Next, we need the short codes for both Brazil and Columbia. Let’s get them:

private String brazilShortCode = "BR";
private String columnbiaShortCode = "CO";

Nice. Next, we have to create an instance of the phone number utility that is available in the libphonenumber library:

import com.google.i18n.phonenumbers.PhoneNumberUtil;

private PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();

Now, we can start playing around with the library. First off, we’ll start the Brazilian phone number list and Brazil’s short code. But before we begin anything, we have to create an instance of the PhoneNumber class which comes with the library. All other operations that we do, will be using this PhoneNumber object. So let’s create that first:

PhoneNumber phoneNumberProto = phoneUtil.parse(inputPhoneNumber, shortCode);

This will be called inside a loop, so the inputPhoneNumber variable will have the phone number from each list, and the shortCode variable will be the short code of the country against which we’re running the test.

To make it more clear, I have a method with the following signature:

validateAndFormatPhoneNumber(String inputPhoneNumber, String shortCode)

and I’m calling this function inside a loop, which is looping through the phone number list, like this:

for (String inputPhoneNumber : brazilPhoneNumbers) {
    validateAndFormatPhoneNumber(inputPhoneNumber, brazilShortCode);
}

From this point on, we’re discussing the statements inside the validateAndFormatPhoneNumber() method. Hope that is clear now.

First, we’ll see if the phone number provided is valid for the country represented by the short code. To do this, we just call one method:

boolean isValid = phoneUtil.isValidNumber(phoneNumberProto);

logger.info("Is phone number valid: " + isValid);

That wasn’t difficult, was it? All the other methods are also this simple. So instead of explaining each one of them, I’ll just put the whole code here and you can try and understand that yourself. If not, definitely reach out in the comments below and I’ll try to help you.

if (phoneNumberProto.hasCountryCode()) {
    logger.info("Country code is present: " + phoneNumberProto.getCountryCode());
} else {
    logger.info("Country code is not present.");
}

if (phoneNumberProto.hasNationalNumber()) {
    logger.info("National number is present: " + phoneNumberProto.getNationalNumber());
} else {
    logger.info("National number is not present.");
}

String formattedPhoneNumber = phoneUtil.format(phoneNumberProto, PhoneNumberUtil.PhoneNumberFormat.E164);

if (formattedPhoneNumber.startsWith("+")) {
    logger.info("Removing leading + from phone number");
    formattedPhoneNumber = formattedPhoneNumber.replace("+", "");
}

logger.info("Formatted phone number: " + formattedPhoneNumber);

That wasn’t difficult to understand, right? Let’s now look at the complete code that I’m using to test this library:

public static void main(String[] args) {
    SpringApplication.run(App.class, args);
}

@Override
public void run(String... args) throws Exception {

    List<String> brazilPhoneNumbers = Arrays.asList(
            "+5521976923932",
            "5561991474841",
            "055 719 9174 8722",
            "719 9174 8722",
            "55 (719) (9174) (8722)"
    );

    List<String> columbiaPhoneNumbers = Arrays.asList(
            "573146339375",
            "3137145988",
            "+573154417054",
            "316537219731323123",
            "3013755140",
            "+57 31 5441 7054",
            "+57 (31) (8393) (1081)"
    );

    phoneUtil = PhoneNumberUtil.getInstance();

    for (String inputPhoneNumber : brazilPhoneNumbers) {
        validateAndFormatPhoneNumber(inputPhoneNumber, brazilShortCode);
    }

    for (String inputPhoneNumber : columbiaPhoneNumbers) {
        validateAndFormatPhoneNumber(inputPhoneNumber, columnbiaShortCode);
    }

    logger.info("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
    logger.info("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
    logger.info("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
    logger.info("Interchanging country code, all numbers should be invalid now.");


    for (String inputPhoneNumber : brazilPhoneNumbers) {
        validateAndFormatPhoneNumber(inputPhoneNumber, columnbiaShortCode);
    }

    for (String inputPhoneNumber : columbiaPhoneNumbers) {
        validateAndFormatPhoneNumber(inputPhoneNumber, brazilShortCode);
    }

}

private void validateAndFormatPhoneNumber(String inputPhoneNumber, String shortCode) {

    logger.info("Processing phone number: " + inputPhoneNumber + " with short code: " + shortCode);

    PhoneNumber phoneNumberProto = null;

    try {
        phoneNumberProto = phoneUtil.parse(inputPhoneNumber, shortCode);

        logger.info("phoneNumberProto: " + new Gson().toJson(phoneNumberProto));

        boolean isValid = phoneUtil.isValidNumber(phoneNumberProto);

        logger.info("Is phone number valid: " + isValid);

        if (phoneNumberProto.hasCountryCode()) {
            logger.info("Country code is present: " + phoneNumberProto.getCountryCode());
        } else {
            logger.info("Country code is not present.");
        }

        if (phoneNumberProto.hasNationalNumber()) {
            logger.info("National number is present: " + phoneNumberProto.getNationalNumber());
        } else {
            logger.info("National number is not present.");
        }

        String formattedPhoneNumber = phoneUtil.format(phoneNumberProto, PhoneNumberUtil.PhoneNumberFormat.E164);

        if (formattedPhoneNumber.startsWith("+")) {
            logger.info("Removing leading + from phone number");
            formattedPhoneNumber = formattedPhoneNumber.replace("+", "");
        }

        logger.info("Formatted phone number: " + formattedPhoneNumber);

    } catch (NumberParseException e) {
        logger.error(e.getMessage());
    }

    logger.info("==================================");
}

As you can see, I have switched the country code at the end to see if the library can figure out the mistake and invalidate everything else. We’ll be able to figure that out from the logs. So let’s check that out.

phoneNumberProto: {"hasCountryCode":true,"countryCode_":55,"hasNationalNumber":true,"nationalNumber_":21976923932,"hasExtension":false,"extension_":"","hasItalianLeadingZero":false,"italianLeadingZero_":false,"hasNumberOfLeadingZeros":false,"numberOfLeadingZeros_":1,"hasRawInput":false,"rawInput_":"","hasCountryCodeSource":false,"countryCodeSource_":"UNSPECIFIED","hasPreferredDomesticCarrierCode":false,"preferredDomesticCarrierCode_":""}
Is phone number valid: true
Country code is present: 55
National number is present: 21976923932
Removing leading + from phone number
Formatted phone number: 5521976923932
==================================
Processing phone number: 5561991474841 with short code: BR
phoneNumberProto: {"hasCountryCode":true,"countryCode_":55,"hasNationalNumber":true,"nationalNumber_":61991474841,"hasExtension":false,"extension_":"","hasItalianLeadingZero":false,"italianLeadingZero_":false,"hasNumberOfLeadingZeros":false,"numberOfLeadingZeros_":1,"hasRawInput":false,"rawInput_":"","hasCountryCodeSource":false,"countryCodeSource_":"UNSPECIFIED","hasPreferredDomesticCarrierCode":false,"preferredDomesticCarrierCode_":""}
Is phone number valid: true
Country code is present: 55
National number is present: 61991474841
Removing leading + from phone number
Formatted phone number: 5561991474841
==================================
Processing phone number: 055 719 9174 8722 with short code: BR
phoneNumberProto: {"hasCountryCode":true,"countryCode_":55,"hasNationalNumber":true,"nationalNumber_":71991748722,"hasExtension":false,"extension_":"","hasItalianLeadingZero":false,"italianLeadingZero_":false,"hasNumberOfLeadingZeros":false,"numberOfLeadingZeros_":1,"hasRawInput":false,"rawInput_":"","hasCountryCodeSource":false,"countryCodeSource_":"UNSPECIFIED","hasPreferredDomesticCarrierCode":false,"preferredDomesticCarrierCode_":""}
Is phone number valid: true
Country code is present: 55
National number is present: 71991748722
Removing leading + from phone number
Formatted phone number: 5571991748722
==================================
Processing phone number: 719 9174 8722 with short code: BR
phoneNumberProto: {"hasCountryCode":true,"countryCode_":55,"hasNationalNumber":true,"nationalNumber_":71991748722,"hasExtension":false,"extension_":"","hasItalianLeadingZero":false,"italianLeadingZero_":false,"hasNumberOfLeadingZeros":false,"numberOfLeadingZeros_":1,"hasRawInput":false,"rawInput_":"","hasCountryCodeSource":false,"countryCodeSource_":"UNSPECIFIED","hasPreferredDomesticCarrierCode":false,"preferredDomesticCarrierCode_":""}
Is phone number valid: true
Country code is present: 55
National number is present: 71991748722
Removing leading + from phone number
Formatted phone number: 5571991748722
==================================
Processing phone number: 55 (719) (9174) (8722) with short code: BR
phoneNumberProto: {"hasCountryCode":true,"countryCode_":55,"hasNationalNumber":true,"nationalNumber_":71991748722,"hasExtension":false,"extension_":"","hasItalianLeadingZero":false,"italianLeadingZero_":false,"hasNumberOfLeadingZeros":false,"numberOfLeadingZeros_":1,"hasRawInput":false,"rawInput_":"","hasCountryCodeSource":false,"countryCodeSource_":"UNSPECIFIED","hasPreferredDomesticCarrierCode":false,"preferredDomesticCarrierCode_":""}
Is phone number valid: true
Country code is present: 55
National number is present: 71991748722
Removing leading + from phone number
Formatted phone number: 5571991748722
==================================
Processing phone number: 573146339375 with short code: CO
phoneNumberProto: {"hasCountryCode":true,"countryCode_":57,"hasNationalNumber":true,"nationalNumber_":3146339375,"hasExtension":false,"extension_":"","hasItalianLeadingZero":false,"italianLeadingZero_":false,"hasNumberOfLeadingZeros":false,"numberOfLeadingZeros_":1,"hasRawInput":false,"rawInput_":"","hasCountryCodeSource":false,"countryCodeSource_":"UNSPECIFIED","hasPreferredDomesticCarrierCode":false,"preferredDomesticCarrierCode_":""}
Is phone number valid: true
Country code is present: 57
National number is present: 3146339375
Removing leading + from phone number
Formatted phone number: 573146339375
==================================
Processing phone number: 3137145988 with short code: CO
phoneNumberProto: {"hasCountryCode":true,"countryCode_":57,"hasNationalNumber":true,"nationalNumber_":3137145988,"hasExtension":false,"extension_":"","hasItalianLeadingZero":false,"italianLeadingZero_":false,"hasNumberOfLeadingZeros":false,"numberOfLeadingZeros_":1,"hasRawInput":false,"rawInput_":"","hasCountryCodeSource":false,"countryCodeSource_":"UNSPECIFIED","hasPreferredDomesticCarrierCode":false,"preferredDomesticCarrierCode_":""}
Is phone number valid: true
Country code is present: 57
National number is present: 3137145988
Removing leading + from phone number
Formatted phone number: 573137145988
==================================
Processing phone number: +573154417054 with short code: CO
phoneNumberProto: {"hasCountryCode":true,"countryCode_":57,"hasNationalNumber":true,"nationalNumber_":3154417054,"hasExtension":false,"extension_":"","hasItalianLeadingZero":false,"italianLeadingZero_":false,"hasNumberOfLeadingZeros":false,"numberOfLeadingZeros_":1,"hasRawInput":false,"rawInput_":"","hasCountryCodeSource":false,"countryCodeSource_":"UNSPECIFIED","hasPreferredDomesticCarrierCode":false,"preferredDomesticCarrierCode_":""}
Is phone number valid: true
Country code is present: 57
National number is present: 3154417054
Removing leading + from phone number
Formatted phone number: 573154417054
==================================
Processing phone number: 316537219731323123 with short code: CO
The string supplied is too long to be a phone number.
==================================
Processing phone number: 3013755140 with short code: CO
phoneNumberProto: {"hasCountryCode":true,"countryCode_":57,"hasNationalNumber":true,"nationalNumber_":3013755140,"hasExtension":false,"extension_":"","hasItalianLeadingZero":false,"italianLeadingZero_":false,"hasNumberOfLeadingZeros":false,"numberOfLeadingZeros_":1,"hasRawInput":false,"rawInput_":"","hasCountryCodeSource":false,"countryCodeSource_":"UNSPECIFIED","hasPreferredDomesticCarrierCode":false,"preferredDomesticCarrierCode_":""}
Is phone number valid: true
Country code is present: 57
National number is present: 3013755140
Removing leading + from phone number
Formatted phone number: 573013755140
==================================
Processing phone number: +57 31 5441 7054 with short code: CO
phoneNumberProto: {"hasCountryCode":true,"countryCode_":57,"hasNationalNumber":true,"nationalNumber_":3154417054,"hasExtension":false,"extension_":"","hasItalianLeadingZero":false,"italianLeadingZero_":false,"hasNumberOfLeadingZeros":false,"numberOfLeadingZeros_":1,"hasRawInput":false,"rawInput_":"","hasCountryCodeSource":false,"countryCodeSource_":"UNSPECIFIED","hasPreferredDomesticCarrierCode":false,"preferredDomesticCarrierCode_":""}
Is phone number valid: true
Country code is present: 57
National number is present: 3154417054
Removing leading + from phone number
Formatted phone number: 573154417054
==================================
Processing phone number: +57 (31) (8393) (1081) with short code: CO
phoneNumberProto: {"hasCountryCode":true,"countryCode_":57,"hasNationalNumber":true,"nationalNumber_":3183931081,"hasExtension":false,"extension_":"","hasItalianLeadingZero":false,"italianLeadingZero_":false,"hasNumberOfLeadingZeros":false,"numberOfLeadingZeros_":1,"hasRawInput":false,"rawInput_":"","hasCountryCodeSource":false,"countryCodeSource_":"UNSPECIFIED","hasPreferredDomesticCarrierCode":false,"preferredDomesticCarrierCode_":""}
Is phone number valid: true
Country code is present: 57
National number is present: 3183931081
Removing leading + from phone number
Formatted phone number: 573183931081
==================================
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Interchanging country code, all numbers should be invalid now.
Processing phone number: +5521976923932 with short code: CO
phoneNumberProto: {"hasCountryCode":true,"countryCode_":55,"hasNationalNumber":true,"nationalNumber_":21976923932,"hasExtension":false,"extension_":"","hasItalianLeadingZero":false,"italianLeadingZero_":false,"hasNumberOfLeadingZeros":false,"numberOfLeadingZeros_":1,"hasRawInput":false,"rawInput_":"","hasCountryCodeSource":false,"countryCodeSource_":"UNSPECIFIED","hasPreferredDomesticCarrierCode":false,"preferredDomesticCarrierCode_":""}
Is phone number valid: true
Country code is present: 55
National number is present: 21976923932
Removing leading + from phone number
Formatted phone number: 5521976923932
==================================
Processing phone number: 5561991474841 with short code: CO
phoneNumberProto: {"hasCountryCode":true,"countryCode_":57,"hasNationalNumber":true,"nationalNumber_":5561991474841,"hasExtension":false,"extension_":"","hasItalianLeadingZero":false,"italianLeadingZero_":false,"hasNumberOfLeadingZeros":false,"numberOfLeadingZeros_":1,"hasRawInput":false,"rawInput_":"","hasCountryCodeSource":false,"countryCodeSource_":"UNSPECIFIED","hasPreferredDomesticCarrierCode":false,"preferredDomesticCarrierCode_":""}
Is phone number valid: false
Country code is present: 57
National number is present: 5561991474841
Removing leading + from phone number
Formatted phone number: 575561991474841
==================================
Processing phone number: 055 719 9174 8722 with short code: CO
phoneNumberProto: {"hasCountryCode":true,"countryCode_":57,"hasNationalNumber":true,"nationalNumber_":571991748722,"hasExtension":false,"extension_":"","hasItalianLeadingZero":false,"italianLeadingZero_":false,"hasNumberOfLeadingZeros":false,"numberOfLeadingZeros_":1,"hasRawInput":false,"rawInput_":"","hasCountryCodeSource":false,"countryCodeSource_":"UNSPECIFIED","hasPreferredDomesticCarrierCode":false,"preferredDomesticCarrierCode_":""}
Is phone number valid: false
Country code is present: 57
National number is present: 571991748722
Removing leading + from phone number
Formatted phone number: 57571991748722
==================================
Processing phone number: 719 9174 8722 with short code: CO
phoneNumberProto: {"hasCountryCode":true,"countryCode_":57,"hasNationalNumber":true,"nationalNumber_":71991748722,"hasExtension":false,"extension_":"","hasItalianLeadingZero":false,"italianLeadingZero_":false,"hasNumberOfLeadingZeros":false,"numberOfLeadingZeros_":1,"hasRawInput":false,"rawInput_":"","hasCountryCodeSource":false,"countryCodeSource_":"UNSPECIFIED","hasPreferredDomesticCarrierCode":false,"preferredDomesticCarrierCode_":""}
Is phone number valid: false
Country code is present: 57
National number is present: 71991748722
Removing leading + from phone number
Formatted phone number: 5771991748722
==================================
Processing phone number: 55 (719) (9174) (8722) with short code: CO
phoneNumberProto: {"hasCountryCode":true,"countryCode_":57,"hasNationalNumber":true,"nationalNumber_":5571991748722,"hasExtension":false,"extension_":"","hasItalianLeadingZero":false,"italianLeadingZero_":false,"hasNumberOfLeadingZeros":false,"numberOfLeadingZeros_":1,"hasRawInput":false,"rawInput_":"","hasCountryCodeSource":false,"countryCodeSource_":"UNSPECIFIED","hasPreferredDomesticCarrierCode":false,"preferredDomesticCarrierCode_":""}
Is phone number valid: false
Country code is present: 57
National number is present: 5571991748722
Removing leading + from phone number
Formatted phone number: 575571991748722
==================================
Processing phone number: 573146339375 with short code: BR
phoneNumberProto: {"hasCountryCode":true,"countryCode_":55,"hasNationalNumber":true,"nationalNumber_":573146339375,"hasExtension":false,"extension_":"","hasItalianLeadingZero":false,"italianLeadingZero_":false,"hasNumberOfLeadingZeros":false,"numberOfLeadingZeros_":1,"hasRawInput":false,"rawInput_":"","hasCountryCodeSource":false,"countryCodeSource_":"UNSPECIFIED","hasPreferredDomesticCarrierCode":false,"preferredDomesticCarrierCode_":""}
Is phone number valid: false
Country code is present: 55
National number is present: 573146339375
Removing leading + from phone number
Formatted phone number: 55573146339375
==================================
Processing phone number: 3137145988 with short code: BR
phoneNumberProto: {"hasCountryCode":true,"countryCode_":55,"hasNationalNumber":true,"nationalNumber_":3137145988,"hasExtension":false,"extension_":"","hasItalianLeadingZero":false,"italianLeadingZero_":false,"hasNumberOfLeadingZeros":false,"numberOfLeadingZeros_":1,"hasRawInput":false,"rawInput_":"","hasCountryCodeSource":false,"countryCodeSource_":"UNSPECIFIED","hasPreferredDomesticCarrierCode":false,"preferredDomesticCarrierCode_":""}
Is phone number valid: true
Country code is present: 55
National number is present: 3137145988
Removing leading + from phone number
Formatted phone number: 553137145988
==================================
Processing phone number: +573154417054 with short code: BR
phoneNumberProto: {"hasCountryCode":true,"countryCode_":57,"hasNationalNumber":true,"nationalNumber_":3154417054,"hasExtension":false,"extension_":"","hasItalianLeadingZero":false,"italianLeadingZero_":false,"hasNumberOfLeadingZeros":false,"numberOfLeadingZeros_":1,"hasRawInput":false,"rawInput_":"","hasCountryCodeSource":false,"countryCodeSource_":"UNSPECIFIED","hasPreferredDomesticCarrierCode":false,"preferredDomesticCarrierCode_":""}
Is phone number valid: true
Country code is present: 57
National number is present: 3154417054
Removing leading + from phone number
Formatted phone number: 573154417054
==================================
Processing phone number: 316537219731323123 with short code: BR
The string supplied is too long to be a phone number.
==================================
Processing phone number: 3013755140 with short code: BR
phoneNumberProto: {"hasCountryCode":true,"countryCode_":55,"hasNationalNumber":true,"nationalNumber_":3013755140,"hasExtension":false,"extension_":"","hasItalianLeadingZero":false,"italianLeadingZero_":false,"hasNumberOfLeadingZeros":false,"numberOfLeadingZeros_":1,"hasRawInput":false,"rawInput_":"","hasCountryCodeSource":false,"countryCodeSource_":"UNSPECIFIED","hasPreferredDomesticCarrierCode":false,"preferredDomesticCarrierCode_":""}
Is phone number valid: false
Country code is present: 55
National number is present: 3013755140
Removing leading + from phone number
Formatted phone number: 553013755140
==================================
Processing phone number: +57 31 5441 7054 with short code: BR
phoneNumberProto: {"hasCountryCode":true,"countryCode_":57,"hasNationalNumber":true,"nationalNumber_":3154417054,"hasExtension":false,"extension_":"","hasItalianLeadingZero":false,"italianLeadingZero_":false,"hasNumberOfLeadingZeros":false,"numberOfLeadingZeros_":1,"hasRawInput":false,"rawInput_":"","hasCountryCodeSource":false,"countryCodeSource_":"UNSPECIFIED","hasPreferredDomesticCarrierCode":false,"preferredDomesticCarrierCode_":""}
Is phone number valid: true
Country code is present: 57
National number is present: 3154417054
Removing leading + from phone number
Formatted phone number: 573154417054
==================================
Processing phone number: +57 (31) (8393) (1081) with short code: BR
phoneNumberProto: {"hasCountryCode":true,"countryCode_":57,"hasNationalNumber":true,"nationalNumber_":3183931081,"hasExtension":false,"extension_":"","hasItalianLeadingZero":false,"italianLeadingZero_":false,"hasNumberOfLeadingZeros":false,"numberOfLeadingZeros_":1,"hasRawInput":false,"rawInput_":"","hasCountryCodeSource":false,"countryCodeSource_":"UNSPECIFIED","hasPreferredDomesticCarrierCode":false,"preferredDomesticCarrierCode_":""}
Is phone number valid: true
Country code is present: 57
National number is present: 3183931081
Removing leading + from phone number
Formatted phone number: 573183931081
==================================

As you can see, it’s not 100% accurate, but 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 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 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.

Become a Patron!
Sunny Srinidhi
Coding, reading, sleeping, listening, watching, potato. INDIAN. "If you don't have time to do it right, when will you have time to do it over?" - John Wooden
https://blog.contactsunny.com

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Top