Regular Expression Phone Number validation in Java

Validating a phone number using regular expression is tricky because the phone number can be written in many formats and can have extensions also.

For example, here are some of the common way of writing phone numbers:

  • 1234567890
  • 123-456-7890
  • 123-456-7890 x1234
  • 123-456-7890 ext1234
  • (123)-456-7890
  • 123.456.7890
  • 123 456 7890

Here I am using java regular expressions to validate any of these format phone numbers.

package com.journaldev.util;

public class PhoneNumberValidator {
	
	
	public static void main(String[] args) {
		System.out.println("Phone number 1234567890 validation result: "+validatePhoneNumber("1234567890"));
		System.out.println("Phone number 123-456-7890 validation result: "+validatePhoneNumber("123-456-7890"));
		System.out.println("Phone number 123-456-7890 x1234 validation result: "+validatePhoneNumber("123-456-7890 x1234"));
		System.out.println("Phone number 123-456-7890 ext1234 validation result: "+validatePhoneNumber("123-456-7890 ext1234"));
		System.out.println("Phone number (123)-456-7890 validation result: "+validatePhoneNumber("(123)-456-7890"));
		System.out.println("Phone number 123.456.7890 validation result: "+validatePhoneNumber("123.456.7890"));
		System.out.println("Phone number 123 456 7890 validation result: "+validatePhoneNumber("123 456 7890"));
	}

	private static boolean validatePhoneNumber(String phoneNo) {
		//validate phone numbers of format "1234567890"
		if (phoneNo.matches("\\d{10}")) return true;
		//validating phone number with -, . or spaces
		else if(phoneNo.matches("\\d{3}[-\\.\\s]\\d{3}[-\\.\\s]\\d{4}")) return true;
		//validating phone number with extension length from 3 to 5
		else if(phoneNo.matches("\\d{3}-\\d{3}-\\d{4}\\s(x|(ext))\\d{3,5}")) return true;
		//validating phone number where area code is in braces ()
		else if(phoneNo.matches("\\(\\d{3}\\)-\\d{3}-\\d{4}")) return true;
		//return false if nothing matches the input
		else return false;
		
	}

}

Here is the output of the above program:

Phone number 1234567890 validation result: true
Phone number 123-456-7890 validation result: true
Phone number 123-456-7890 x1234 validation result: true
Phone number 123-456-7890 ext1234 validation result: true
Phone number (123)-456-7890 validation result: true
Phone number 123.456.7890 validation result: true
Phone number 123 456 7890 validation result: true

The best part of this program is that it’s easy to understand and you can extend it easily to support other phone number formats also.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Current ye@r *

Subscribe to JournalDev Newsletter
Get the FREE access to Monthly Newsletter and Free PDF eBooks
*No Spam Guaranteed. By entering your email address, you agree also subscribing to our newsletter.
Oops! - Something went wrong.
Close
Oracle Magazine for Developers Download Now
Exclusive Offer: Citrix™ Whitepaper on Denial of Service Attack Download Now