mardi 29 septembre 2015

How do I go through the String to make sure it only contains numbers in certain places

I am writing something that will take a user's input for a phone number and tell the user if their input was valid or not based on five parameters:

1) input is 13 characters in length

2) char at index 0 is '('

3) char at index 4 is ')'

4) char at index 8 is '-'

5) All other characters must be one of the digits: ’0’ through ’9’ inclusive.

So far I have everything down except the 5th parameter. My code for that goes as followed

   if (number.contains("[0-9]+"))
    {
        ints = true;

        if (number.contains("[a-zA-Z]*\\d+."))
        {
            ints = false;
        }
    }
    else 
    {
        ints = false;
    }

(Side note: number is my string that is the user's input, and ints is a boolean declared earlier in the code).

Aucun commentaire:

Enregistrer un commentaire