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

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
pavani
Guest
pavani

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

ijal
Guest
ijal

How should i ask user to enter only 60

paresh
Guest
paresh

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

STEWY
Guest
STEWY

HOW DO I CHECK FOR A NPA OR NXX NUMBER ?

Farhad Tarapore
Guest
Farhad Tarapore

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.

guillermo
Guest
guillermo

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

satya
Guest
satya

Thank a lot
Nice example

Shobha
Guest
Shobha

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

Rocky
Guest
Rocky

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

Andre
Guest
Andre

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
Guest
Prateek Ashtikar

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

trackback
Telephone number formatting – validation « BAOA: BloggingAboutOracleApplications | Everything about Oracle…and it's applications!
James
Guest
James

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

Bryce
Guest
Bryce
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… Read more »