HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Regular Expressions / Java regex to validate international phone numbers

Java regex to validate international phone numbers

In this regex tutorial, we will learn to validate international phone numbers based on industry-standard notation specified by ITU-T E.123

The rules and conventions used to print international phone numbers vary significantly around the world, so it’s hard to provide meaningful validation for an international phone number unless you adopt a strict format. Fortunately, there is a simple, industry-standard notation specified by ITU-T E.123. This notation requires that international phone numbers include a leading plus sign (known as the international prefix symbol), and allows only spaces to separate groups of digits.

Also thanks to the international phone numbering plan (ITU-T E.164), phone numbers cannot contain more than 15 digits. The shortest international phone numbers in use contain seven digits.

1. Regex to Validate International Phone Numbers

Regex : ^\+(?:[0-9] ?){6,14}[0-9]$

^ # Assert position at the beginning of the string.
 \+ # Match a literal "+" character.
 (?: # Group but don't capture:
 [0-9] # Match a digit.
 \\s # Match a space character
 ? # between zero and one time.
 ) # End the noncapturing group.
 {6,14} # Repeat the group between 6 and 14 times.
 [0-9] # Match a digit.
 $ # Assert position at the end of the string.

Above regular expression can be used to validate international phone numbers based on ITU-T standards. Let’s look at one example.

List phoneNumbers = new ArrayList();
phoneNumbers.add("+1 1234567890123");
phoneNumbers.add("+12 123456789");
phoneNumbers.add("+123 123456");

String regex = "^\\+(?:[0-9] ?){6,14}[0-9]$";

Pattern pattern = Pattern.compile(regex);

for(String email : phoneNumbers)
{
	Matcher matcher = pattern.matcher(email);
	System.out.println(email +" : "+ matcher.matches());
}

Output:

+1 1234567890123 : true
+12 123456789 : true
+123 123456 : true

2. Validate international phone numbers in EPP format

This regular expression follows the international phone number notation specified by the Extensible Provisioning Protocol (EPP). EPP is a relatively recent protocol (finalized in 2004), designed for communication between domain name registries and registrars. It is used by a growing number of domain name registries, including .com, .info, .net, .org, and .us. The significance of this is that EPP-style international phone numbers are increasingly used and recognized, and therefore provide a good alternative format for storing (and validating) international phone numbers.

EPP-style phone numbers use the format +CCC.NNNNNNNNNNxEEEE, where C is the 1–3 digit country code, N is up to 14 digits, and E is the (optional) extension. The leading plus sign and the dot following the country code are required. The literal “x” character is required only if an extension is provided.

Regex : ^\+[0-9]{1,3}\.[0-9]{4,14}(?:x.+)?$

List phoneNumbers = new ArrayList();
phoneNumbers.add("+123.123456x4444");
phoneNumbers.add("+12.1234x11");
phoneNumbers.add("+1.123456789012x123456789");

String regex = "^\\+[0-9]{1,3}\\.[0-9]{4,14}(?:x.+)?$";

Pattern pattern = Pattern.compile(regex);

for(String email : phoneNumbers)   
{
	Matcher matcher = pattern.matcher(email);
	System.out.println(email +" : "+ matcher.matches());
}

Output:

+123.123456x4444 : true
+12.1234x11 : true
+1.123456789012x123456789 : true

Feel free to edit above regex and play with it to match more strict phone number formats, you have in your mind.

Happy Learning !!

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Paul Johny

    October 31, 2019

    Hi,
    How to validate the phone number with country code and exactly 10 digits.

    would it be “^\\+(?:[0-9] ?){10}[0-9]$”
    the above one doesn’t work for me

    can someone help me out

    eg +91 9876543213

    the “9876543213” part should be exactly 10 digits

Comments are closed on this article!

Search Tutorials

Java Regex Tutorial

  • Java Regex – Introduction
  • Java Regex : Alphanumeric
  • Java Regex : Credit Card Numbers
  • Java Regex : Canadian Postcodes
  • Java Regex : Currency Symbol
  • Java Regex : Date Format
  • Java Regex : Email Address
  • Java Regex : Password
  • Java Regex : Greek Characters
  • Java Regex : ISBNs
  • Java Regex : Min/Max Length
  • Java Regex : No. of Lines
  • Java Regex : No. of Words
  • Java Regex : SSN
  • Java Regex : UK Postal Codes
  • Java Regex : US Postal Codes
  • Java Regex : Trademark Symbol
  • Java Regex : Intl Phone Numbers
  • North American Phone Numbers

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)