Javatpoint Logo
Javatpoint Logo

Mobile Number Validation in Java

Each country has its own mobile number format. It is difficult to validate mobile numbers for each country. So, in this section, we will learn how to validate mobile numbers in Java using regular expression and Google's libphonenumber API. In this section, we will create Java programs to validate Indian and US number formats only. On the basis of these two countries, we can validate mobile numbers for other countries by making some modifications in the code.

US Mobile Numbers Formats

Indian Mobile Number Formats

How to Validate Mobile Number?

There are two ways to validate mobile number in Java:

  • Using Regular Expression
    • Using Pattern Class
    • Using String.matches() Method
  • Using Google libphonenumber API

Using Regular Expression

Using Pattern Class

The Pattern class belongs to java.util.regex package. It provides the compile() method that compiles the given regular expression into a pattern. It accepts a string of regular expressions to be compiled.

Syntax:

Another method is provided by the class is matcher() which creates a matcher that will match the given input against this pattern. It accepts character sequence to be matched.

Syntax:

Another class that we have used in the following program is the Matcher class. It is a final class that that performs match operations on a character sequence by interpreting a Pattern. Note that a matcher is created from the pattern.

The class provides the find() method that finds the next subsequence of the input sequence that matches the pattern. It starts at the beginning of this matcher's region, or if a previous invocation of the method was successful. It returns true, if and only if a subsequence of the input sequence matches this matcher's pattern.

The group() method of the class returns the input subsequence matched by the previous match.

The equals(obj) method of the String class compares the string to the specified object and returns true if and only if the argument is not null and is a string object.

MobileNumberValidation.java

Output:

It is a valid mobile number.

Using String.matches() Method

In the following program, we have used the matches() method of the Java String class. It tells that the string matches with regular expression or not.

Syntax:

The method accepts a parameter of String type known as a regular expression and returns a Boolean value. It throws PatternSyntaxException if regular expression syntax is invalid.

MobileNumberValidation1.java

Output:

Mobile Number Validation in Java

If we go with the above approach, we need to do lots of testing work to cover all the cases. But if we have some API that provides this functionality with proper testing that would be good to use in our application.

Using Google libphonenumber API

The libphonenumber is an open-source library provided by Google. It provides functionalities such as parsing, formatting, validating, and storing international phone numbers. The Java version of the library is optimized for running on smartphones and is used by the Android framework since 4.0.

The API supports the following types of numbers:

  • FIXED_LINE
  • FIXED_LINE_OR_MOBILE
  • MOBILE
  • PAGER
  • PERSONAL_NUMBER
  • PREMIUM_RATE
  • SHARED_COST
  • TOLL_FREE
  • UAN
  • UNKNOWN
  • VOICEMAIL
  • VOIP

Note: Before running the program, ensure that libphonenumber-8.12.24.jar file is added to the project.

In the following program, we have used PhoneNumberUtil class that provides getInstance(), isValidNumber() and parse() method.

MobileNumberValidation2.java

Output:

Mobile Number Validation in Java





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA