3

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.

Sangram Anand
  • 10,526
  • 23
  • 70
  • 103
  • 1
    Is "-----------)))))" a valid phone number? Your regex would say it is ;) I'll try to add an answer with a more complete approach. Also, I think you need to escape those parenthesis AND that dot inside your character class... otherwise you'll match anything. – pcalcao Feb 23 '12 at 11:43
  • http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation – John Eipe Feb 23 '12 at 11:44
  • 1
    http://blog.stevenlevithan.com/archives/validate-phone-number – John Eipe Feb 23 '12 at 11:45
  • Wouldn't `[0-9.()-]` match any character anyway? – biziclop Feb 23 '12 at 11:45
  • 1
    You can further restrict the RegEx. But you should generate some testcases before using international numbers with +, spaces, etc. and specify if it's valid or not. Then you can test your RegEx on all that test strings. – Tarion Feb 23 '12 at 11:47
  • Have double escpaed the whitespace as in `\\s` ? – devsnd Feb 24 '12 at 14:33

5 Answers5

7

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.

bew
  • 631
  • 3
  • 4
3

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.

Brian
  • 6,391
  • 3
  • 33
  • 49
1

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

}
sw1nn
  • 7,278
  • 1
  • 26
  • 36
0

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.

Eric Ruck
  • 671
  • 6
  • 6
0

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.

Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83