Regular Expression Phone Number validation in Java

Filed Under: 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

Phone Number Validation in Java

Here I am using java regular expressions to validate any of the above 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 phone number regex validation 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.

You can checkout more regular expressions examples from our GitHub Repository.

Comments

  1. DDH says:

    Great but not 100% correct, if you input something like 123-456.7890, it still accept because
    [-\\.\\s] mean the program will accept if “-” OR “.” OR “white space”. My solution is separate it into 3 if statement, but i would love to know if there are different solution out there

  2. Harish says:

    Need a regex to match Indian phone number of format +91-1234567890. Please suggest

    1. joyofcodes says:

      “^(\\+91?)[789]{1}\\d{9}$”
      discription:
      checks +91, then digits between 7-9 then validates 9 digits no in the end. hope this helps!

    2. joyofcodes says:

      package validation;

      import java.util.Scanner;
      import java.util.regex.Matcher;
      import java.util.regex.Pattern;
      import javax.swing.JOptionPane;

      public class Validation {

      public static void main(String[] args) {

      //using scanner function you can read sentences entered from the user
      Scanner sc = new Scanner(System.in);

      //declaring boolean z and instantiating it to true.
      Boolean z = true;
      //telling the user to enter their phone no
      System.out.println(“enter your phone: “);
      //storing the entered phone no inside an object called “phone” which is of String data-type.
      String phone = sc.nextLine();

      //defining an object called regix_phone(you can use any name) which is of String data-type.
      String regix_phone = “^(\\+91?)[789]{1}\\d{9}$”;

      //compiling the pattern(i.e String regix_phone) declared above.
      Pattern p3 = Pattern.compile(regix_phone);
      //using a matches function we match the object of “Pattern p3” as declared above.
      Matcher m3 = p3.matcher(phone);

      //checking whether our entered phone No is true of false using Boolean function as declared above(“Boolean z”)
      if(m3.matches() != z)// this condition checks if they do not match and then return false and do nothing.
      {
      System.out.println(“invalid”);
      }
      else // else if the conditon check is true then return true and Print “valid”
      System.out.println(“valid”);
      }

      }

  3. roopal says:

    I need to validate the mobile number, it should be 10 digit and its does not accept less the 10

  4. usha says:

    It was really helpful, saved my day, thanks

  5. Deepak says:

    Can u post phone number validation using regex in java working example

  6. Deepak says:

    Where is the ValidatorPhoneNumber method.it is not here on this tutorial

  7. Kranthi says:

    Can you tell me all the digits should not be equal..ex-9999999999 then it should return false.

  8. Steve says:

    Thank you. Very helpful!

  9. Akwinlopez says:

    I need to validate the mobile number, it should be 10 digit and its starts from either 7,8 or 9

    1. durga reddy says:

      Pattern p = Pattern.compile(“[789]{1}\\d{9}”);
      Matcher matcher = p.matcher(“9849112345”);
      System.out.println( matcher.matches());

  10. Santhosh says:

    Thanks for your information pankaj….

  11. kamila says:

    can u pls post a single regex pattern that can take all these combination

    1. Pankaj says:

      You can use Regex OR operator for a single regex pattern.

  12. JBKIK6G says:

    19967 178977I

    1. Pankaj says:

      I didn’t understood your comment.

  13. suraj says:

    thanks for the simple explanation.
    very easy to get how it works

    1. Pankaj says:

      Thanks Suraj for liking it.

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages