In this Java tutorial, you will learn how to validate a given phone number using String.matches() method, with examples.
Validate Phone Number in Java
To validate phone number, the easiest way is to use a regular expression and apply string matching using String.matches() method.
Different countries have different formats for phone numbers. We shall present you examples on how to handle some of those. And you can build your own required format by going through these examples.
In this tutorial, we will learn how to validate different formats of phone numbers using regular expressions.
Validate Phone Number using Regular Expression
In this example, we will get a phone number in a string. Then we shall define a regular expression, that matches the phone numbers only with a correct format.
We use String.matches() method and pass regular expression as argument. String.matches() returns the boolean value true if this string matches with the regular expression, else it returns false.
Example.java
public class Example {
public static void main(String[] args) {
isValidPhoneNumber("754-3010");
isValidPhoneNumber("75 3010");
}
/**
* Method to validate phone number of format NNN-NNNN, example 754-3010
* @param phone_number
* @return true if phone_number is valid, false if not
*/
public static boolean isValidPhoneNumber(String phone_number) {
boolean isValid = phone_number.matches("\\d{3}-\\d{4}");
System.out.println(phone_number+" : "+isValid);
return isValid;
}
}
The regular expression "\\d{3}-\\d{4}"
matches with a string that starts with three digits followed by a hyphen and then four immediate digits. Only those strings that match this format shall yield true. For instance, "754-3010"
yields true while "75 3010"
yields false as shown in the following console output.
Output
754-3010 : true
75 3010 : false
Validating Phone number of Format NNN-NNN-NNN-NNNN
In this example, we shall learn to validate phone numbers of format: three digits, hyphen, three digits, hyphen, three digits, hyphen, four digits.
Example.java
/**
* Java Example Program to validate phone number
*/
public class Example {
public static void main(String[] args) {
isValidPhoneNumber("001-541-754-3010");
isValidPhoneNumber("755-253-3010");
}
/**
* Method to validate phone number of format NNN-NNN-NNN-NNNN, example 001-541-754-3010
* @param phone_number
* @return true if phone_number is valid, false if not
*/
public static boolean isValidPhoneNumber(String phone_number) {
boolean isValid = phone_number.matches("\\d{3}-\\d{3}-\\d{3}-\\d{4}");
System.out.println(phone_number+" : "+isValid);
return isValid;
}
}
Output
001-541-754-3010 : true
755-253-3010 : false
Validating Phone Number with format of 10 digits
In this example, we shall validate a phone number with 10 digits of format NNNNNNNNNN which is used in India.
Example.java
/**
* Java Example Program to validate phone number
*/
public class Example {
public static void main(String[] args) {
isValidPhoneNumber("9876543210");
isValidPhoneNumber("7533010");
}
/**
* Method to validate phone number of format NNNNNNNNNN, example 9876543210
* @param phone_number
* @return true if phone_number is valid, false if not
*/
public static boolean isValidPhoneNumber(String phone_number) {
boolean isValid = phone_number.matches("\\d{10}");
System.out.println(phone_number+" : "+isValid);
return isValid;
}
}
Output
9876543210 : true
7533010 : false
Conclusion
To sum up, in this Java Tutorial, we learned how to validate phone numbers in Java using regular expression. We have gone through only some of the patterns, but you can make the pattern of phone number using regular expression, which matches the format you would like to validate based on the country or some specific requirement.