In this post, we will see how to validate phone number in java.
Table of Contents
Validate any international phone number as per E.123
E.123 is a standards-based recommendation by the International Telecommunications Union sector ITU-T.
E.123 provides following specifications.
- The leading plus (+) serves as an international prefix symbol, and is immediately followed by the country code and then phone number.
- Spaces should separate country code, area code and local number.
Regex
^\+(?:[0-9] ?){6,14}[0-9]$
Explanation
^ –> Assert position at the beginning of the string.
\+ –> Match a literal “+” character.
(?: –> Group but don’t capture:
[0-9] –> Match a digit from 0-9.
\x20 –> Match a space character(used x20 to show space character)
? –> between zero and one time.
) –> End the noncapturing group.
{6,14} –> Recur the group between 6 and 14 times.
[0-9] –> Match digit from 0-9
$ –> Assert position at the end of the string.
\+ –> Match a literal “+” character.
(?: –> Group but don’t capture:
[0-9] –> Match a digit from 0-9.
\x20 –> Match a space character(used x20 to show space character)
? –> between zero and one time.
) –> End the noncapturing group.
{6,14} –> Recur the group between 6 and 14 times.
[0-9] –> Match digit from 0-9
$ –> Assert position at the end of the string.
Java program to check phone number
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package org.arpit.java2blog; // Java program to check phone // is valid as per E123 import java.util.regex.Matcher; import java.util.regex.Pattern; public class ValidatePhoneNumberMain { public static boolean isValidE123(String s) { Pattern p = Pattern.compile("^\\+(?:[0-9] ?){6,14}[0-9]$"); Matcher m = p.matcher(s); return (m.find() && m.group().equals(s)); } public static void main(String[] args) { String phone1 = "+91 3423 546443"; String phone2 = "+44 343 2324"; String phone3 = "91 4354 3454"; String[] phoneNumbers= {phone1,phone2,phone3}; for (int i = 0; i < phoneNumbers.length; i++) { String phoneNumber=phoneNumbers[i]; if (isValidE123(phoneNumber)) System.out.print(phoneNumber+" is valid phone number"); else System.out.print(phoneNumber+" is invalid Phone number"); System.out.println(); } } } |
Output
+91 3423 546443 is valid phone number
+44 343 2324 is valid phone number
91 4354 3454 is invalid Phone number
+44 343 2324 is valid phone number
91 4354 3454 is invalid Phone number
Validate Indian mobile number
Valid indian mobile numbers are:
9876543210
09876543210
919876543210
0919876543210
+919876543210
+91-9876543210
0091-9876543210
+91 -9876543210
+91- 9876543210
+91 – 9876543210
0091 – 9876543210
09876543210
919876543210
0919876543210
+919876543210
+91-9876543210
0091-9876543210
+91 -9876543210
+91- 9876543210
+91 – 9876543210
0091 – 9876543210
Regex
^(?:(?:\\+|0{0,2})91(\\s*[\\-]\\s*)?|[0]?)?[789]\\d{9}$
Explanation
I would recommend to go to site https://regex101.com/. Put the regex in regular expression. It will show you clear explanation of regex.
Here is java program for the validate indian mobile number
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package org.arpit.java2blog; // Java program to check phone // is valid as per E123 import java.util.regex.Matcher; import java.util.regex.Pattern; public class ValidatePhoneNumberMain { public static boolean isValidIndianMobileNumber(String s) { Pattern p = Pattern.compile("^(?:(?:\\+|0{0,2})91(\\s*[\\-]\\s*)?|[0]?)?[789]\\d{9}$"); Matcher m = p.matcher(s); return (m.find() && m.group().equals(s)); } public static void main(String[] args) { String phone1 = "+91-7123456789"; String phone2 = "08123456789"; String phone3 = "9876543210"; String[] phoneNumbers= {phone1,phone2,phone3}; for (int i = 0; i < phoneNumbers.length; i++) { String phoneNumber=phoneNumbers[i]; if (isValidIndianMobileNumber(phoneNumber)) System.out.print(phoneNumber+" is valid mobile number"); else System.out.print(phoneNumber+" is invalid mobile number"); System.out.println(); } } } |
Output
+91-7123456789 is valid mobile number
08123456789 is valid mobile number
9876543210 is valid mobile number
08123456789 is valid mobile number
9876543210 is valid mobile number
That’s all about validate phone number in java.
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.