-3

I need to do a phone number validation that the tel# contains only: numbers 0-9 left and right parenthesis hyphen blank space

in c#

this is my code for now:

//ValidatePhoneNumber
    public static string ValidatePhoneNumber(string v_strPhoneNumber)
    {
        const string strPHONE_NUMBER_BLANK =
           "Phone Number cannot be blank";
        const string strPHONE_NUMBER_TOO_LONG =
            "Phone Number cannot be longer than 24 characters";

        if (v_strPhoneNumber.Trim().Length == 0)
            return strPHONE_NUMBER_BLANK;
        if (v_strPhoneNumber.Trim().Length > 24)
            return trPHONE_NUMBER_TOO_LONG;

        return String.Empty; 
    }//end ValidatePhoneNumber

I need to add that validation into that code, I am a beginer so the easiest way possible would be great. If you can give me the answer please if you could at least point me in the right direction, Thanks!

Kara
  • 6,115
  • 16
  • 50
  • 57
  • @dognose `(123) 456 7890` is not a valid number but may be a valid phone number. How can this be duplicate of it? – L.B Mar 22 '14 at 23:46
  • 1
    I would go this route: http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation – attila Mar 22 '14 at 23:48
  • If you need just numbers, I wouldn't validate it, but rather just remove everything else automatically. Then, afterwards, you can validate the number itself, if you need to. – GolezTrol Mar 23 '14 at 00:03

1 Answers1

0

This the RegEx that I use for telephone number validation

^((| |)\d{3}()| |)(-| )\d{3}-\d{4}$

You just need to adjust it to your needs.

CheGueVerra
  • 7,849
  • 4
  • 37
  • 49