Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java phone number validation

Here is my problem:

Create a constructor for a telephone number given a string in the form xxx-xxx-xxxx or xxx-xxxx for a local number. Throw an exception if the format is not valid.

So I was thinking to validate it using a regular expression, but I don't know if I'm doing it correctly. Also what kind of exception would I have to throw? Do I need to create my own exception?

    public TelephoneNumber(String aString){
        if(isPhoneNumberValid(aString)==true){
            StringTokenizer tokens = new StringTokenizer("-");
            if(tokens.countTokens()==3){
                areaCode = Integer.parseInt(tokens.nextToken());
                exchangeCode = Integer.parseInt(tokens.nextToken());
                number = Integer.parseInt(tokens.nextToken());
            }
            else if(tokens.countTokens()==2){
                exchangeCode = Integer.parseInt(tokens.nextToken());
                number = Integer.parseInt(tokens.nextToken());
            }
            else{
                //throw an excemption here
            }
        }

    }


 public static boolean isPhoneNumberValid(String phoneNumber){
     boolean isValid = false;

     //Initialize reg ex for phone number.
    String expression = "(\\d{3})(\\[-])(\\d{4})$";
    CharSequence inputStr = phoneNumber;
    Pattern pattern = Pattern.compile(expression);
    Matcher matcher = pattern.matcher(inputStr);
    if(matcher.matches()){
        isValid = true;
     }
        return isValid;
    }

Hi sorry, yes this is homework. For this assignments the only valid format are xxx-xxx-xxxx and xxx-xxxx, all other formats (xxx)xxx-xxxx or xxxxxxxxxx are invalid in this case.

I would like to know if my regular expression is correct

like image 640
user69514 Avatar asked Mar 31 '10 17:03

user69514


People also ask

How do you check if a number is a valid mobile number?

Step #1: Enter the number you wish to validate and select the country of provenance. Click on the “Validate Number” button to instantly validate the phone number. Step #2: The 'Main Info' section displays information about the number's country, type and the carrier.

How do I validate a 10 digit mobile number?

you can also use jquery for this length==10){ var validate = true; } else { alert('Please put 10 digit mobile number'); var validate = false; } } else { alert('Not a valid number'); var validate = false; } if(validate){ //number is equal to 10 digit or number is not string enter code here... } Save this answer.

How do I validate a phone number in spring?

PhoneNumber(value)). fetch(); return true; If the phone number is not valid, the Lookup API returns a 404 which is surfaced in code as an ApiException , so if we reach the return true then it was a valid phone number. If not we enter the catch block and return false if the response code was 404.


2 Answers

So I was thinking to validate it using a regular expression, but I don't know if I'm doing it correctly.

It indeed looks overcomplicated. Also, matching xxx-xxx-xxxx or xxx-xxxx where x is a digit can be done better with "(\\d{3}-){1,2}\\d{4}". To learn more about regex I recommend to go through http://regular-expressions.info.

Also what kind of exception would I have to throw? Do I need to create my own exception?

A ValidatorException seems straight forward.

public static void isPhoneNumberValid(String phoneNumber) throws ValidatorException {
    if (!phoneNumber.matches(regex)) {
        throws ValidatorException("Invalid phone number");
    }
}

If you don't want to create one yourself for some reasons, then I'd probably pick IllegalArgumentException, but still, I don't recommend that.

That said, this validation of course doesn't cover international and/or external telephone numbers. Unless this is really homework, I'd suggest to rethink the validation.

like image 157
BalusC Avatar answered Oct 10 '22 07:10

BalusC


^(([(]?(\d{2,4})[)]?)|(\d{2,4})|([+1-9]+\d{1,2}))?[-\s]?(\d{2,3})?[-\s]?((\d{7,8})|(\d{3,4}[-\s]\d{3,4}))$

matches: (0060)123-12345678, (0060)12312345678, (832)123-1234567, (006)03-12345678,

(006)03-12345678, 00603-12345678, 0060312345678

0000-123-12345678, 0000-12-12345678, 0000-1212345678 ... etc.

1234-5678, 01-123-4567

Can replace '-' with SPACE i.e (0080) 123 12345678

Also matches +82-123-1234567, +82 123 1234567, +800 01 12345678 ... etc.

More for house-hold/private number. Not for 1-800-000-0000 type of number

*Tested with Regex tester http://regexpal.com/

like image 43
peterong Avatar answered Oct 10 '22 07:10

peterong