Validate phone number in java

In this post, we will see how to validate phone number in java.


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.

Java program to check phone number

Output

+91 3423 546443 is valid 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

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

Output

+91-7123456789 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?

Leave a Reply

Your email address will not be published. Required fields are marked *