The Java RegEx – 10 Digit Mobile Number Validation example shows how to validate a 10 digit mobile number using Java regular expression. It also shows how to validate an 11 digit number having 0 at the start.
How to validate a 10 digit mobile number using a Java regex pattern?
The mobile phone number syntax is fairly simple, at least in India. It should contain all digits and the length must be 10. So basically we want to validate that the input phone number does not have anything except digits 0-9 and the number length must be 10.
Here is the simple pattern to validate this.
1 |
^[0-9]{10}$ |
Where,
1 2 3 4 |
^ - Start of the string [0-9] - Any digit between 0 to 9 {10} - 10 times $ - End of the string |
Let’s test the pattern with some sample phone numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.javacodeexamples.regex; public class TenDigitMobileNumberRegExExample { public static void main(String[] args) { String[] strNumbers = { "90124422342", "655674457", "536776442a", "5445656566" }; String strPattern = "^[0-9]{10}$"; for(String number : strNumbers) { System.out.println(number + " => " + number.matches(strPattern)); } } } |
Output
1 2 3 4 |
90124422342 => false 655674457 => false 536776442a => false 5445656566 => true |
The first number is 11 digits so it fails the validation. The second number is 9 in length, so it fails too. The third number contains the character “a” which is not allowed in the phone number, so it fails. The fourth number is validated successfully.
Our pattern worked for the regular numbers but what about the mobile number “0123456789”? The length of the number is 10 and it only contains digits, so it should be validated using our pattern. However, this number is not valid because it has a leading 0 so basically, it is only 9 digits in length.
1 2 3 4 5 6 7 8 9 |
String[] strNumbers = { "0919293949" }; String strPattern = "^[0-9]{10}$"; for(String number : strNumbers) { System.out.println(number + " => " + number.matches(strPattern)); } |
Output
1 |
0919293949 => true |
To fix this, we can modify our pattern a little bit like given below.
1 |
^[1-9][0-9]{9}$ |
This pattern forces the first digit to be non-zero. The rest of the 9 digits can be anything from 0 to 9.
Final code:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
String[] strNumbers = { "12345678912", "123456789", "536776442a", "0124422342", "5445656566" }; String strPattern = "^[1-9][0-9]{9}$"; for(String number : strNumbers) { System.out.println(number + " => " + number.matches(strPattern)); } |
Output
1 2 3 4 5 |
12345678912 => false 123456789 => false 536776442a => false 0124422342 => false 5445656566 => true |
The first and second number is not 10 in length so both of them fails the validation. The third number contains an alphabet that is not allowed in the mobile number, so it fails as well. The fourth number is 10 in length but starts with a 0, so it fails as well.
RegEx Validation for 11 digits with a leading 0
Some countries allow an additional 0 at the start of the number. For example, in India, we can prepend 0 to any mobile number. In that case, the first digit is “0”, the second digit must be between 1 to 9 and the rest of the 9 digits can be anything from 0 to 9.
Since the starting 0 is optional, we also need to accommodate the 10 digit regular mobile numbers and 11 digit mobile numbers with a leading 0. For this requirement, the pattern could be written as given below.
1 |
^([1-9][0-9]{9})|([0][1-9][0-9]{9})$ |
I have combined two patterns using the “|” (or condition). Here are some of the mobile number validations using this pattern.
1 2 3 4 5 6 7 8 |
12345678912 => false 123456789 => false 536776442a => false 0124422342 => false 5445650566 => true 12345678912 => false 02345678912 => true 00345678912 => false |
This example is part of Java Regular Expression Tutorial with Examples.
Please let me know your views in the comments section below.