How to Validate Phone Number Using Java: Simple Guide
To validate a phone number in Java, use the
Pattern and Matcher classes with a regular expression that matches the desired phone number format. For example, a regex like "^\\+?[0-9]{10,13}$" checks for an optional plus sign followed by 10 to 13 digits.Syntax
Use the Pattern.compile(String regex) method to create a pattern from a regular expression. Then use pattern.matcher(String input) to create a matcher for the input string. Finally, call matcher.matches() to check if the input matches the pattern.
Pattern: Compiles the regex.Matcher: Checks the input against the regex.matches(): Returnstrueif input fully matches the regex.
java
Pattern pattern = Pattern.compile("^\\+?[0-9]{10,13}$"); Matcher matcher = pattern.matcher(phoneNumber); boolean isValid = matcher.matches();
Example
This example shows how to validate phone numbers that may start with a plus sign and contain 10 to 13 digits. It prints whether each phone number is valid or not.
java
import java.util.regex.Pattern; import java.util.regex.Matcher; public class PhoneNumberValidator { public static boolean isValidPhoneNumber(String phoneNumber) { String regex = "^\\+?[0-9]{10,13}$"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(phoneNumber); return matcher.matches(); } public static void main(String[] args) { String[] testNumbers = { "+12345678901", "1234567890", "12345", "+12345678901234", "123-456-7890" }; for (String number : testNumbers) { System.out.println(number + " is valid? " + isValidPhoneNumber(number)); } } }
Output
+12345678901 is valid? true
1234567890 is valid? true
12345 is valid? false
+12345678901234 is valid? false
123-456-7890 is valid? false
Common Pitfalls
Common mistakes include:
- Not anchoring the regex with
^and$, which allows partial matches. - Ignoring formatting characters like spaces, dashes, or parentheses.
- Using a regex that is too strict or too loose for your phone number format.
To handle formatted numbers, you may need to preprocess the input by removing spaces or dashes before validation.
java
/* Wrong: missing anchors allows partial matches */ Pattern wrongPattern = Pattern.compile("\\+?[0-9]{10,13}"); Matcher wrongMatcher = wrongPattern.matcher("abc+1234567890xyz"); boolean wrongResult = wrongMatcher.matches(); // false, but find() would be true /* Right: use anchors to match whole string */ Pattern rightPattern = Pattern.compile("^\\+?[0-9]{10,13}$"); Matcher rightMatcher = rightPattern.matcher("+1234567890"); boolean rightResult = rightMatcher.matches(); // true
Quick Reference
Tips for phone number validation in Java:
- Use
^and$to match the entire string. - Allow optional plus sign with
\+?. - Use
[0-9]{10,13}to specify digit count. - Preprocess input to remove spaces or symbols if needed.
- Test with various valid and invalid inputs.
Key Takeaways
Use Java's Pattern and Matcher classes with regex to validate phone numbers.
Always anchor your regex with ^ and $ to ensure full string matching.
Adjust the regex to match your expected phone number format and length.
Preprocess input to remove formatting characters if your regex does not handle them.
Test your validation with both valid and invalid phone number examples.