Monday, November 18, 2024
Google search engine
HomeLanguagesJavaJava Program to Check For a Valid Mobile Number

Java Program to Check For a Valid Mobile Number

Regular Expressions or Regex (in short) is an API for defining String patterns that can be used for searching, manipulating, and editing a string in Java. Email validation and passwords are few areas of strings where Regex is widely used to define the constraints. Regular Expressions are provided under java.util.regex package. This consists of 3 classes and 1 interface.

Here we will be discussing out two examples, one for India and the other be standard validating over for all countries. Hence, the cases are as follows:

  • Case 1: Local phone numbers that are Indian phone numbers
  • Case 2: Global phone numbers

Case 1: Local phone numbers that are Indian phone numbers  

Mobile Number validation criteria are as follows:

  • The first digit should contain numbers between 7 and 9.
  • The rest 9 digits can contain any number between 0 and 9.
  • The mobile number can have 11 digits also by including 0 at the starting.
  • The mobile number can be of 12 digits also by including 91 at the starting.

Illustration:

Input  : Enter Mobile Number: 7873923408
Output : Valid Mobile Number
Input  : Enter Mobile Number: 5678729234
Output : Invalid Mobile Number

Concept:

One must have a good understanding of regular expression as it is a prerequisite. Mobile number validation in Java is done using Pattern and Matcher classes of Java. The pattern class is used to compile the given pattern/regular expression and the matcher class is used to match the input string with compiled pattern/regular expression. In this method, a regular expression is mentioned, which mentions the criteria for validation of the input string which in this article is a mobile number. The mobile number in India has 10 digits, so we can mention a regular expression that contains only 10 digits.

Example:

Java




// Java Program to Check if Mobile Number is Valid or Not
 
// Importing all regex classes from java.util package to
// match character sequence against patterns
// Importing input output classes
import java.io.*;
import java.util.regex.*;
// Main class
class GFG {
 
    // Method 1
    // To check whether number is valid or not
    public static boolean isValid(String s)
    {
 
        // The given argument to compile() method
        // is regular expression. With the help of
        // regular expression we can validate mobile
        // number.
        // The number should be of 10 digits.
 
        // Creating a Pattern class object
        Pattern p = Pattern.compile("^\\d{10}$");
 
        // Pattern class contains matcher() method
        // to find matching between given number
        // and regular expression for which
        // object of Matcher class is created
        Matcher m = p.matcher(s);
 
        // Returning boolean value
        return (m.matches());
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
 
        // Considering two numbers as inputs
        // Custom input numbers
        String s = "7984286257";
        String s_1 = "5426391";
 
        // Checking over method 1 for string 1
        if (isValid(s))
 
            // Print statement
            System.out.println("Valid Number");
        else
 
            // Print statement
            System.out.println("Invalid Number");
 
        // Again, checking over method 1 for string 1
        if (isValid(s_1))
 
            // Print statement
            System.out.println("Valid Number");
        else
 
            // Print statement
            System.out.println("Invalid Number");
    }
}


Output

Valid Number
Invalid Number

Case 2: Global phone numbers 

The approach is again with the help of regular Expression to match Phone Number of All Country Formats

Mobile number validation criteria: 

  1. The country code prefix starts with ‘+’ and has 1-3 digits.
  2. Most of the countries have the last 4 digits after a hyphen(-).

The numbers which satisfy the validation are shown in illustrations as follows:

Illustration:

Input  : Enter Mobile Number : +1 212 555-3458 (USA)
Output : Valid Mobile Number
Input  : Enter Mobile Number : +4934 351 125-3456
Output : Invalid Mobile Number

Example:

Java




// Java program to Check if Mobile Number is Valid or Not
 
// Importing all regex classes for
// character sequences with pattern matching
import java.util.regex.*;
 
// Main class
class GFG {
 
    // Method 1
    //
    public static boolean isValid(String s)
    {
 
        // The given argument to compile() method
        // is regular expression. With the help of
        // regular expression we can validate mobile
        // number for which we create an object of
        // Pattern class
 
        Pattern p = Pattern.compile(
            "^(\\+\\d{1,3}( )?)?((\\(\\d{1,3}\\))|\\d{1,3})[- .]?\\d{3,4}[- .]?\\d{4}$");
 
        // Pattern class contains matcher() method
        // to find matching between given number
        // and regular expression by creating an object of
        // Matcher class
        Matcher m = p.matcher(s);
 
        // Returns boolean value
        return (m.matches());
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
 
        // Custom input strings
        String s = "+1 212 555-3458";
        String s_1 = "+4934 351 125-3456";
 
        // Checking if the strings are valid or not
        if (isValid(s))
 
            // Print statement
            System.out.println("Valid Number");
        else
 
            // Print statement
            System.out.println("Invalid Number");
 
        // Again, checking over another string(phone number)
        if (isValid(s_1))
 
            // Print statement
            System.out.println("Valid Number");
        else
 
            // Print statement
            System.out.println("Invalid Number");
    }
}


Output

Valid Number
Invalid Number
RELATED ARTICLES

Most Popular

Recent Comments