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.
Main Topics
Browse All TopicsHey, 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?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
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...
Is there a regular expression section in EE? Also, this site: http://www.regexlib.com/Se
Business Accounts
Answer for Membership
by: peterxlanePosted on 2005-10-11 at 10:41:30ID: 15062542
I don't know enough about Regular Expressions to change the expression, but you could put it all in a function and do something like this:
er)
6") & "<br>" 66") & "<br>"
<%
Function ValidatePhone(strNumber)
Dim objRegExpr
Set objRegExpr = New regexp
objRegExpr.Pattern = "((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}"
objRegExpr.Global = True
objRegExpr.IgnoreCase = True
Dim colMatches
Set colMatches = objRegExpr.Execute(strNumb
If colMatches.Count > 0 Then
ValidatePhone = True
Else
If Len(strNumber) = 4 And IsNumeric(strNumber) Then
ValidatePhone = True
Else
ValidatePhone = False
End If
End If
Set colMatches = Nothing
Set objRegExpr = Nothing
End Function
Response.Write ValidatePhone("444-555-666
Response.Write ValidatePhone("(444)555-66
Response.Write ValidatePhone("55556") & "<br>"
Response.Write ValidatePhone("5555") & "<br>"
Response.Write ValidatePhone("hello") & "<br>"
%>