Java Regex to Validate Phone Number
In this article, we’ll learn how to validate mobile phone number of different country’s format using Java Regex (Regular Expressions)
Phone Number Format
A typical mobile phone number has following component:
+<country_code> <area_code> <subscriber_number>
Where depending on the country,
- country_code is somewhere between 1 to 3 digits
- area_code and subscriber_number combined is somewhere between 8 to 11 digits
If you simply require a regex to match all country format then here it is,
"^(\\+\\d{1,3}( )?)?((\\(\\d{1,3}\\))|\\d{1,3})[- .]?\\d{3,4}[- .]?\\d{4}$"
If you want to know how we came up with this regex? then please read this full article.
Regex to match 10 digit Phone Number with No space
This is simplest regex to match just 10 digits. We will also see here how to use regex to validate pattern:
String regex = "^\\d{10}$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher("9876543210");
matcher.matches(); // returns true if pattern matches, else returns false
Let’s break the regex and understand,
^
start of expressiond{10}
is mandatory match of 10 digits without any space$
end of expression
You can make regex more flexible to match between 8 to 11 digits phone number with no space, using this regex:
String noSpaceRegex = "^\\d{8,11}$";
Regex to match 10 digit Phone Number with WhiteSpaces, Hyphens or No space
String spacesAndHyphenRegex = "^(\\d{3}[- ]?){2}\\d{4}$";
Let’s break the regex and understand,
^
start of expressiond{3}
is mandatory match of 3 digits[- ]?
is optional match of whitespace or hyphen after 3 digits{2}
is to repeat the above matchd{3}[- ]?
two times becomes total 6 digitsd{4}
is mandatory match of last 4 digits$
end of expression
This Pattern will match mobile phone numbers like 9876543210, 987 654 3210, 987-654-3210, 987 654-3210, 987 6543210, etc.
Regex to match 10 digit Phone Number with Parentheses
String parenthesesRegex = "^((\\(\\d{3}\\))|\\d{3})[- ]?\\d{3}[- ]?\\d{4}$";
Let’s break the regex and understand,
^
start of expression(\\(\\d{3}\\))|\\d{3})
is mandatory match of 3 digits with or without parentheses[- ]?
is optional match of whitespace or hyphen after after 3 digits\\d{3}[- ]?
is mandatory match of next 3 digits followed by whitespace, hyphen or no space\\d{4}
is mandatory match of last 4 digits$
end of expression
This Pattern will match mobile phone numbers with spaces and hyphen, as well as numbers like (987)6543210, (987) 654-3210, (987)-654-3210 etc.
Regex to match 10 digit Phone number with Country Code Prefix
This regex is combined with regex to include parenthesis
String countryCodeRegex = "^(\\+\\d{1,3}( )?)?((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$";
Let’s break the regex and understand,
^
start of expression(\\+\\d{1,3}( )?)?
is optional match of country code between 1 to 3 digits prefixed with + symbol, followed by space or no space.((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}
is mandatory match of 10 digits with or without parenthesis, followed by whitespace, hyphen or no space$
end of expression
This Pattern will match mobile phone numbers from previous examples as well as numbers like +91 (987)6543210, +111 (987) 654-3210, +66 (987)-654-3210 etc.
Regex to match Phone Number of All Country Formats
Before we start defining a regex, let’s look at some of the country phone number formats:-
Abkhazia +995 442 123456
Afghanistan +93 30 539-0605
Australia +61 2 1255-3456
China +86 (20) 1255-3456
Germany +49 351 125-3456
Indonesia +62 21 6539-0605
Iran +98 (515) 539-0605
Italy +39 06 5398-0605
New Zealand +64 3 539-0605
Philippines +63 35 539-0605
Singapore +65 6396 0605
Thailand +66 2 123 4567
UK +44 141 222-3344
USA +1 (212) 555-3456
Vietnam +84 35 539-0605
Let’s extract some information from these numbers:-
- Country Code prefix starts with ‘+’ and has 1 to 3 digits
- Last part of the number, also known as subscriber number is 4 digits in all of the numbers
- Most of the countries have 10 digits phone number after excluding country code. A general observation is that all countries phone number falls somewhere between 8 to 11 digits after excluding country code.
Let’s see again on regex from previous example to validate country code:
String countryCodeRegex = "^(\\+\\d{1,3}( )?)?((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$"l
The above regex is to match 10 digit phone numbers, Let’s make some changes in this and make it more flexible to match 8 to 11 digits phone numbers:-
Regex to Match All Country Formats
String allCountryRegex = "^(\\+\\d{1,3}( )?)?((\\(\\d{1,3}\\))|\\d{1,3})[- .]?\\d{3,4}[- .]?\\d{4}$";
Let’s break the regex and understand,
^
start of expression(\\+\\d{1,3}( )?)?
is optional match of country code between 1 to 3 digits prefixed with ‘+’ symbol, followed by space or no space.((\\(\\d{1,3}\\))|\\d{1,3}
is mandatory group of 1 to 3 digits with or without parenthesis followed by hyphen, space or no space.\\d{3,4}[- .]?
is mandatory group of 3 or 4 digits followed by hyphen, space or no space\\d{4}
is mandatory group of last 4 digits$
end of expression
Regex to match Phone Number of Specific Country Format
As you saw in the previous example that we have to add some flexibility in our regex to match all countries’ formats. If you want more strict validation of specific country format then here are examples of India and Singapore Phone number regex pattern:
String indiaRegex = "^(\\+\\d{2}( )?)?((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$";
String singaporeRegex = "^(\\+\\d{2}( )?)?\\d{4}[- .]?\\d{4}$";