Below is the Java Code which checks whether the Mobile Number is Valid or Not. In my case Mobile Number is considered valid if it begins with 0 or 91, then contains 7 or 8 or 9, and then contains 9 digits. So, if a number contains this numbers, then it is considered as valid else not valid.
// Java program to check if given mobile number
// is valid.
import java.util.regex.*;
import java.util.Scanner;
class MobileNoValidation {
public static void main(String[] args)
{
String number = "347873923408"
if (isNumberValid(number))
System.out.println("Valid Number");
else
System.out.println("Invalid Number");
}
public static boolean isNumberValid(String number)
{
/* In India, Mobile is valid if below 3 condition is specified
* 1) Begins with 0 or 91
* 2) Then contains 7 or 8 or 9.
* 3) Then contains 9 digits
* You can change this validation as you want
*/
Pattern p = Pattern.compile("(0/91)?[7-9][0-9]{9}");
Matcher m = p.matcher(number);
return (m.find() && m.group().equals(number));
// find() method Attempts to find the next subsequence of the input sequence that matches the pattern.
// group() method attempts to find the next subsequence of the input sequence that matches the pattern
}
}