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");
      }
 }
}

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

17 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
guillermo
13 years ago

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

ijal
10 years ago

How should i ask user to enter only 60

Farhad Tarapore
12 years ago

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.

satya
13 years ago

Thank a lot
Nice example

Shobha
14 years ago

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

Rocky
14 years ago

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

Andre
14 years ago

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

Prateek Ashtikar
11 years ago
Reply to  Andre

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

James
15 years ago

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

Nii
8 years ago
Reply to  mkyong

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.

Bryce
16 years ago

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.

Rahul
16 years ago
Reply to  mkyong

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

pavani
8 years ago

I want phone number starts with 7,8,9
can u say regex for it

paresh
10 years ago

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

STEWY
11 years ago

HOW DO I CHECK FOR A NPA OR NXX NUMBER ?