Link to home
Start Free TrialLog in
Avatar of Tyedyeguy
Tyedyeguy

asked on

Phone number validation using a regular expression

Hey, I'm trying to validate a phone number field on a form, but have minimal knowledge of regular expressions. Here's what I'm currently using as a validation expression: ((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}

It allows the following formats: (555)555-5555, 555-5555, and 555-5555. However, the number could also be a department number from within the company, in which case I would like to allow just a 4 digit number such as 5555. What alterations would I have to make?
ASKER CERTIFIED SOLUTION
Avatar of peterxlane
peterxlane

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tyedyeguy
Tyedyeguy

ASKER

Well, for simplicity's sake, I'd much prefer to just use a regular expression in a validator control. If nobody else has any answers, I'll go with your example.
Personally, I would probably create a JavaScript (or search for one) that strips out all characters that are not numeric, and then validates that what is left is either 10 digits or 4 digits.  This could all be done on the client side.

Also, I noticed that your regular expression seems to recognize all sorts of combinations of numbers as long as there is a hyphen in there somewere...

Are you using .Net validator controls?
Sorry about the delay. Yep, .Net validators is what I'm using. I tried a few combinations of numbers and hyphens, and they all fail unless it's an actual phone number format. Do you know of a specific wrong format that is recognized as true?
Is there a regular expression section in EE?  Also, this site:  http://www.regexlib.com/Search.aspx has a search for regular expressions, but I think adding in that criteria to allow users to type the four digit number will require someone that actually knows how to work with these expressions...
Well, turns out the validation is no longer requested to be used. But since you posted an alternative solution, the points are yours.