Sangram Anand
Sangram Anand

Reputation: 10854

Phone number validation in Java

I am using the following code to validate a phone number. The requirements are the phone number should be inbetween 10-25 characters length, should include hypen(-),period(.), parentheses ().

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


public class ValidatePhoneNumber {

public static void main(String[] argv) {

    String phoneNumber = "6058.8()6-05888,9994567";
    System.out.println(phoneNumber.length());
    //String sPhoneNumber = "605-88899991";
    //String sPhoneNumber = "605-888999A";
    String regex = "^[0-9.()-]{10,25}$";
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(phoneNumber);

    if (matcher.matches()) {
        System.out.println("Phone Number Valid");
    } else {
        System.out.println("Phone Number must be in the form XXX-XXXXXXX");
    }
  }
 }

I checked the validations and its working fine, I want to add a whitespace character "\s" as well so that in between the phone number there can be whitespace as well. But getting errors while adding "\s" in regex.

Upvotes: 3

Views: 7911

Answers (5)

Eric Ruck
Eric Ruck

Reputation: 671

I use this regex: "^(1[\s-]?)?(((\d{3}))|(\d{3}))[\s-]?\d{3}[\s-]?\d{4}$" It's probably more than I really need, especially since I work on mobile so people rarely use parens or dashes, but it makes me happy.

Upvotes: 0

bew
bew

Reputation: 641

Please, look at standards like ITU E.164 or IETF RfC 3966. Don't assume that every country has the same conventions and number lengths.

Here's the relevant ABNF part out of RfC 3966

global-number-digits = "+" *phonedigit DIGIT *phonedigit
phonedigit           = DIGIT / visual-separator 
visual-separator     = "-" / "." / "(" / ")"
DIGIT = "0" / "1" / "2" / "3" / "4" / "5" / "6" / "7" / "8" / "9"

We recently had a huge trouble with a device that didn't let the user enter the '+' as part of a phone number.

Upvotes: 7

sw1nn
sw1nn

Reputation: 7328

You have this in a main() method, presumably for the purposes of posting here. However, if this code is run multiple times you should consider moving the regex compilation outside the called method. Compilation is potentially expensive.

Note that java.util.regex.Pattern is thread safe, java.util.regex.Matcher is not so the standard idiom is something like:

public class PhoneNumberValidator {

    private static Pattern VALID_PHONE_NUMBER = 
        Pattern.compile("^[0-9.()-]{10,25}$");

    public boolean isValidPhoneNumber(String s) {

         Matcher m = VALID_PHONE_NUMBER.matcher(s);

         return m.matches();
    }

}

Upvotes: 1

Mike Thomsen
Mike Thomsen

Reputation: 37506

Probably something like this would be better

"\\(?[\\d]{3}\\)?\\-?[\\d]{w}\\-?[\\d]{4}"

Don't know how international that would be, but it should give you an idea. The problem with yours is that it would capture a lot of weird number, hyphen and parenthesis combinations.

Upvotes: 0

Brian
Brian

Reputation: 6450

I would suggest not being so fussy with phone numbers, people like to use spaces and brackets in their own ways, and such strictness can simply irritate. Why not restrict it to numbers only, but otherwise spaces/brackets etc are OK?

Also - sometimes international numbers use a '+' char.

Upvotes: 3

Related Questions