Phone Formatting Demo w/Regular Expressions

This demo illustrates how to format a 10 digit number into a phone format ((XXX) XXX-XXXX) using regular expressions.

Enter 10 consecutive digits: (i.e., 8005554321)

Source Code
<form method="get" action="phoneFormat.asp" id=form1 name=form1>
  <b>Enter 10 consecutive digits:</b> (<i>i.e., 8005554321</i>)<br />
  <input type="text" name="txtPhone" value="<%=Request("txtPhone")%>">
  <input type="submit" value="Format the Phone Number" id=submit1 name=submit1>

If Len(Request("txtPhone")) > 0 then
  Response.Write "The formatted phone number is: "
  Dim re
  Set re = New RegExp
  re.Pattern = "(\d{3})(\d{3})(\d{4})"
  Response.Write re.Replace(Request("txtPhone"), "($1) $2-$3")
  Set re = Nothing
End If

[Return to the article]