How to validate phone number in Java (regular expression)

Regular expression pattern in Java always is the best method to validate an user’s phone number. Here i provide a regex pattern to determines if the phone number is in correct format, the pattern force starting with 3 digits follow by a “-” and 7 digits at the end.

\\d{3}-\\d{7}

Explanation
\\d = only digit allow
{3} = length

All phone numbers must in “xxx-xxxxxxx” format. For example
1) 012-6677889 – Passed
2) 01216677889 – Failed , “-” missing
3) A12-6677889 – Failed , only digit allow
4) 012-66778899 – Failed, only 7 digits at the end

Full source code of phone number validation in Java


import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ValidatePhoneNumber {
  public static void main(String[] argv) {

      String sPhoneNumber = "605-8889999";
      //String sPhoneNumber = "605-88899991";
      //String sPhoneNumber = "605-888999A";

      Pattern pattern = Pattern.compile("\\d{3}-\\d{7}");
      Matcher matcher = pattern.matcher(sPhoneNumber);
      
      if (matcher.matches()) {
    	  System.out.println("Phone Number Valid");
      }
      else
      {
    	  System.out.println("Phone Number must be in the form XXX-XXXXXXX");
      }
 }
}

17 comments on “How to validate phone number in Java (regular expression)

  1. 08 123 1234
    (408) 123-1234
    408-123-1234
    its not working validation pls help me

    Reply
  2. That’s pretty restrictive. I am looking for a way to allow phone number like:
    1 408 123 1234
    1(408) 123-1234 and combinations thereof e.g. 1(408) 123 1234, 1 (408)-123-1234…
    1-408-123-1234 and combinations thereof

    Simplest way I can think of is

    a) Remove all spaces
    b) Remove all dashes
    c) Remove all parentheses

    What is left should consist only of numbers. Anything else would result in a validation error.

    Reply
  3. Struggled all day and night trying to figure out a solution for a college project. Thanks for the pointers.

    Reply
  4. Pls give me some idea 2 write telephone line checking code in java

    Reply
  5. I’m new to springs can someone help me validate a contact number in Spring .
    Thanks.

    Reply
  6. hi i want to know how should be the pattern if i want the user to enter a number begining with 21 or 27 followed by 6 numbers

    please help me
    Thanks

    Reply
    1. Pattern pattern = Pattern.compile(“^(21)-\d{7}”);
      Matcher matcher = pattern.matcher(sPhoneNumber);

      Reply
  7. Unfortunately this works for US based numbers only. Not everyone in the world has the same style phone number.

    Reply
      1. Hi Sir,

        I am trying to validate US phone number in the form of 555-555-5555 and its working fine and if this is not matching its giving error message is fine.
        But, its failing when I submit the form if the area code of US is not matching. I need to validate area code also before form submit. Can you please help me on this. Error am getting for reference “Phone number (US)324-765-4567 is not valid: Number 324-765-4567 belongs to a non valid area code”. Here 324 is not the area code of US.

        Reply
  8. I created a phone number pattern a little different. I’m trying to verify it’s validity. ^(\d?\d?\d?\d)?(\d{3})(\d{7})$

    First I have a method that strips non-numbers out of the string because there are all kinds of formats people use including parenthesis dashes, periods, etc.

    My regular expression matches first 7 digits. It’s in a group so I can pull it out if I want. Next I match 3 digits for the area code. Again it is grouped so that I can pull it out and use it or store it however I want it. Then I have an optional group for the country code. If I do have the group, then I have at least one digit with 3 more optional digits.

    Any comments suggestions?

    I’ve written some basic unit tests, but I might not be testing some edge cases.

    Reply
    1. your pattern is great, pattern contains unlimited possibilities, and just make sure you make a detail unit test to cover all your use case

      Reply
      1. hi, i want to check that no. must be of 10 digits with first digit non-zero, how to write RE for this ?

        Reply

Leave a Comment

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