Validating phone numbers is a common prerequisite in today’s web, mobile, or desktop applications, but Java does not have an integrated method for carrying out this kind of common validation. So, we have to use some open source libraries to perform such validation. One such library is Google’s phone number library. It helps to verify any phone number, whether foreign, specific to India, or specific to any country.
We can also use regular expressions to validate the phone numbers, but it requires some skills to write such complex expressions and then the testing would be an endless task.
The libphonenumber is an open-source library from Google for formatting, parsing, and validating international phone numbers. It contains many methods to achieve such functionality. Some of them are discussed below: Return Type Method Description format(Phonenumber.PhoneNumber number, PhoneNumberUtil.PhoneNumberFormat numberFormat)
String Uses the default rules to format a phone number in the format specified. String formatNumberForMobileDialing(Phonenumber.PhoneNumber number, java.lang.String regionCallingFrom, boolean withFormatting) Returns the number in a formatted string such that it can be dialed from a cell phone in that region. boolean isMobileNumberPortableRegion(java.lang.String regionCode) If the region passed as an argument supports mobile number portability, the method returns true. boolean isNumberMatch(Phonenumber.PhoneNumber firstNumberIn, Phonenumber.PhoneNumber secondNumberIn) It takes two phone number and checks them for equality boolean isPossibleNumber(java.lang.CharSequence number, java.lang.String regionDialingFrom) Verify that a telephone number is a possible number given in the form of a string and the region from which the number can be dialed. boolean isValidNumberForRegion(Phonenumber.PhoneNumber number, java.lang.String regionCode) Checks whether the given number is valid for a particular region boolean isValidNumber(Phonenumber.PhoneNumber number) Validates whether a phone number matches a specific pattern boolean canBeInternationallyDialled(Phonenumber.PhoneNumber number) Returns true in case the number can be dialed from outside the specified region int getCountryCodeForRegion(java.lang.String regionCode) Returns the country calling code for a specific region PhoneNumberUtil.PhoneNumberType getNumberType(Phonenumber.PhoneNumber number) This method returns the type of number based on the number itself. For example, Toll-Free, Mobile, FixedLine, etc.
This is a rich library with even more utility features and takes care of much of the needs of our program.
Below is Java Implementation for Validating Phone Numbers using Google’s libphonenumber Library. Here we will be using Eclipse IDE.
Step 1: Creating a Maven Project
To begin with first create a Maven Project in Eclipse. The reason behind creating a Maven Project rather than a normal Java Project is that the libphonenumber library is present in the Maven Repository so we have to use it as a dependency in our project.
Leave everything to be the default. The Artifact Id will be the name of your Maven project.
Step 2: Adding the Dependency
Once you have created the Maven Project, add the libphonenumber dependency in the pom.xml file. As soon as you save the file, the library will be downloaded for offline use.
Step 3: Creating the Driver Class
Now, simply create a Java class to use the functionalities of this library.
Type: MOBILE +91 94483 76473 is valid. Type: TOLL_FREE 1800 425 3800 is valid. Type: UNKNOWN +91 83944 7484 is not valid. Type: FIXED_LINE 0294 2424447 is valid.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.