Java Program to Check For a Valid Mobile Number
Last Updated :
05 Dec, 2024
In Java, validating mobile numbers is usually done by using Regular Expressions (Regex) that define patterns for matching strings. Mobile number validation can be implemented for both local (Indian) and global phone numbers.
Example: Now, let's take a basic example, to check if a mobile number is valid based on a simple rule. A valid mobile number must start with a digit between 7 and 9, followed by 9 more digits.
Java
// Java Program to Check For a Valid Mobile Number
import java.util.regex.*;
public class Geeks {
public static void main(String[] args)
{
// Example mobile number
String n = "9991234567";
// Regular expression to check if
// the number starts with
// 7, 8, or 9, followed by 9 digits
String r = "^[7-9][0-9]{9}$";
// Check if the number matches
// the regular expression
if (n.matches(r)) {
System.out.println("Valid Mobile Number");
}
else {
System.out.println("Invalid Mobile Number");
}
}
}
OutputValid Mobile Number
Explanation: In the above example, "^[7-9]" e
nsures the number starts with a digit between 7 and 9. "[0-9]{9}" e
nsures that the next 9 digits are any digits between 0 and 9. "$" d
enotes the end of the string, ensuring that the number is exactly 10 digits long.
Mobile Number Validation for Different Formats in Java
1. Validating Indian Mobile Numbers
Criteria:
- 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.
Example:
Java
// Java Program to Check the Indian Mobile Numbers
import java.util.regex.*;
public class ValidateMobileNum {
public static void main(String[] args) {
// Example mobile numbers
String[] n = { "7873923408", "5678729234" };
// Regular expression to check if the number starts with
// 7, 8, or 9, followed by 9 digits
String r = "^[7-9][0-9]{9}$";
// Iterate through the numbers and check
// if they are valid or invalid
for (String number : n) {
if (number.matches(r)) {
System.out.println(number + ": Valid Mobile Number");
} else {
System.out.println(number + ": Invalid Mobile Number");
}
}
}
}
Output7873923408: Valid Mobile Number
5678729234: Invalid Mobile Number
Notes:
- Valid numbers start with digits 7, 8, or 9 and have exactly 10 digits.
- Invalid numbers either start with a digit not in the range 7-9 or do not have exactly 10 digits.
2. Validating Global Mobile Numbers
Criteria:
- The country code prefix starts with '+' and has 1-3 digits.
- Most of the countries have the last 4 digits after a hyphen(-).
Example:
Java
// Java program to Validating Global Mobile Numbers
import java.util.regex.*;
public class GFG {
// Method to check if mobile number is valid
public static boolean isValid(String s) {
// Regular expression to validate
// global mobile numbers
Pattern p = Pattern.compile(
"^(\\+\\d{1,3}( )?)?((\\(\\d{1,3}\\))|\\d{1,3})[- .]?\\d{3,4}[- .]?\\d{4}$");
// Matcher to check if the string
// matches the regular expression
Matcher m = p.matcher(s);
// Returns true if valid, otherwise false
return (m.matches());
}
public static void main(String[] args) {
String s1 = "+1 212 555-3458";
String s2 = "+4934 351 125-3456";
// Check if both numbers are valid
if (isValid(s1))
System.out.println("Valid Mobile Number");
else
System.out.println("Invalid Mobile Number");
if (isValid(s2))
System.out.println("Valid Mobile Number");
else
System.out.println("Invalid Mobile Number");
}
}
OutputValid Mobile Number
Invalid Mobile Number
Explanation:
"^(\\+\\d{1,3}( )?)?"
: This checks for an optional country code that starts with +
followed by 1 to 3 digits."((\\(\\d{1,3}\\))|\\d{1,3})"
: Validates the area code, which can either be in parentheses or just digits."[- .]?\\d{3,4}[- .]?\\d{4}$"
: Matches the rest of the phone number with optional separators like spaces, dashes, or periods.
Similar Reads
Java program to read all mobile numbers present in given file In this article, we are going to discuss a Java program that reads all mobile numbers from a given text file and writes the correct mobile number to another output file. For this, we are going to use regular expression to identify the correct mobile number. Note: The number should meet the correct c
3 min read
Java Program for Phone Mnemonics In our daily lives, we have to remember a lot of phone numbers. Most people find it quite difficult to remember such huge 10-digit phone numbers. A simple solution is to relate that phone number to some known word. For example, we can relate the phone number 32627287 with DAMASCUS based on the chara
4 min read
Check if a number is binary or not in Java Given a number N, the task is to check first whether the given number is binary or not and its value should be greater than 1. print true if N is the binary representation else print false. Examples: Input: N = 1010 Output: true Explanation: Given number is greater than 1 and none of its digits is g
3 min read
Java Program to Validate Phone Numbers using Google's libphonenumber Library 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
4 min read
How to Match Phone Numbers in a List to a Regex Pattern in Java ? Regular Expressions are also known as regex or regexp. It is one of the powerful features of Java that helps in sequences of characters that define a search pattern. Nowadays it is widely used in computer science, programming, and text processing for tasks such as string matching, data extraction, a
4 min read