For developing any software application we need to validate the inputs entered by the user. It is a common task for developers in any programming language.

In java, we have regular expression, in which we can validate a different set of inputs like phone, Name, DOB, Credit Cards, etc.

Table of Content :

Validating a phone number using regular expression is tricky because the phone number can be written in many formats and can also contain extensions.

String Afghanistan = "+93 30 539-0605"; String Australia = "+61 2 1255-3456";
String China = "+86 (20) 1255-3456";
String Germany = "+49 351 125-3456";
String India = "+91 9876543210";
String Indonesia = "+62 21 6539-0605";
String Iran = "+98 (515) 539-0605";
String Italy = "+39 06 5398-0605";
String NewZealand = "+64 3 539-0605";
String Philippines = "+63 35 539-0605";
String Singapore = "+65 6396 0605";
String Thailand = "+66 2 123 4567";
String UK = "+44 141 222-3344";
String USA = "+1 (212) 555-3456";
String Vietnam = "+84 35 539-0605";

International phone number regex - Example 1

For the phone number validation we need to take 3 or 4 different patterns and combine them with "|":

String phoneRegex = "\\d{10}|(?:\\d{3}-){2}\\d{4}|\\(\\d{3}\\)\\d{3}-?\\d{4}";

  • \d{10} matches 1234567890
  • (?:\d{3}-){2}\d{4} matches 123-456-7890
  • \(\d{3}\)\d{3}-?\d{4} matches (123)456-7890 or (123)4567890
import java.util.regex. * ; public class PhoneNumberValidator { private static String phoneRegex = "\\d{10}|(?:\\d{3}-){2}\\d{4}|\\(\\d{3}\\)\\d{3}-?\\d{4}"; private static String phoneNo = "1234567890"; public static void main(String[] args) { if (phoneNo.matches(phoneRegex)) { System.out.println("Phone number 1234567890 is valid."); } else { System.out.println("Phone number 1234567890 is Invalid."); } } } output: Phone number 1234567890 is valid.

International phone number regex - Example 2

import java.util.regex. * ; public class PhoneNumberValidator2 { private static String phoneRegex = "\\d{10}|\\d{3}[-\\.\\s]\\d{3}[-\\.\\s]\\d{4}|\\d{3}-\\d{3}-\\d{4}\\s(x|(ext))\\d{3,5}|\\(\\d{3}\\)-\\d{3}-\\d{4}"; public static void main(String[] args) { System.out.println("Phone number 1234567890 is valid : " + validatePhone("12345678902")); System.out.println("Phone number 123-456-7890 is valid : " + validatePhone("123-456-7890")); System.out.println("Phone number 123-456-7890 x1234 is valid : " + validatePhone("123-456-7890 x1234")); System.out.println("Phone number 123-456-7890 ext1234 is valid : " + validatePhone("123-456-7890 ext1234")); System.out.println("Phone number (123)-456-7890 is valid : " + validatePhone("(123)-456-7890")); System.out.println("Phone number 123.456.7890 is valid : " + validatePhone("123.456.7890")); System.out.println("Phone number 123 456 7890 is valid : " + validatePhone("123 456 7890")); System.out.println("Phone number 123 456 78904 is valid : " + validatePhone("123 456 78904")); } private static boolean validatePhone(String phoneNo) { if (phoneNo.matches(phoneRegex)) { return true; } else { return false; } } } output: Phone number 1234567890 is valid : false Phone number 123-456-7890 is valid : true Phone number 123-456-7890 x1234 is valid : true Phone number 123-456-7890 ext1234 is valid : true Phone number (123)-456-7890 is valid : true Phone number 123.456.7890 is valid : true Phone number 123 456 7890 is valid : true Phone number 123 456 78904 is valid : false

International phone number regex - Example 3

import java.util.regex. * ; public class PhoneNumberValidator3 { private static String phoneRegex = "^\\s?((\\+[1-9]{1,4}[ \\-]*)|(\\([0-9]{2,3}\\)[ \\-]*)|([0-9]{2,4})[ \\-]*)*?[0-9]{3,4}?[ \\-]*[0-9]{3,4}?\\s?"; private static String phoneNo = " +33 - 123 456 789 "; public static void main(String[] args) { if (Pattern.compile(phoneRegex).matcher(phoneNo).matches()) { System.out.println(phoneNo + " is a valid phone number."); } else { System.out.println(phoneNo + " is a Invalid phone number."); } } } output: +33 - 123 456 789 is a valid phone number.

Post/Questions related to  Match any character or set of Characters - Java Regex

How to check special characters in java using Regex?
How to make regex case insensitive in java?
How to extract a substring from a string in java using Regex?
What does mean in regex Java?
What is a regular expression in Java?
How to escape special characters in java Regex?
Validate international phone numbers in Java using regex
Java regex to allow only alphanumeric characters
Credit Card Number Validation using Regex
Currency Symbols Validation using Regex
Date validation Validation using Regex
Email validation Validation using Regex
Password Validation Validation using Regex
Greek Extended or Greek Script Validation using Regex
Validate ISBN Validation using Regex
Check Min/Max Length of Input Text Validation using Regex
limit the number of lines in text Validation using Regex
Limit the number of words in input Validation using Regex 
Validate SSN (Social Security Numbers) Validation using Regex
Trademark Symbol Validation using Regex
North American Phone Numbers Validation using Regex

In this article, we have seen how to  Validate international phone numbers in Java using regex with examples. All source code in the article can be found in the GitHub repository.