<%
***** begin function area *****
formats a given 10 digit number into a nice looking phone number
example: given strnumber of 8005551212 you get (800) 555-1212
function formatphonenumber(strnumber)
dim strinput string to hold our entered number
dim strtemp temporary string to hold our working text
dim strcurrentchar var for storing each character for eval.
dim i looping var
uppercase all characters for consistency
strinput = ucase(strnumber)
to be able to handle some pretty bad formatting we strip out
all characters except for chars a to z and digits 0 to 9
before proceeding. i left in the chars for stupid slogan
numbers like 1-800-get-cash etc…
for i = 1 to len(strinput)
strcurrentchar = mid(strinput, i, 1)
numbers (0 to 9)
if asc("0") <= asc(strcurrentchar) and asc(strcurrentchar) <= asc("9") then
strtemp = strtemp & strcurrentchar
end if
upper case chars (a to z)
if asc("a") <= asc(strcurrentchar) and asc(strcurrentchar) <= asc("z") then
strtemp = strtemp & strcurrentchar
end if
next i
swap strtemp back to strinput for next set of validation
i also clear strtemp just for good measure!
strinput = strtemp
strtemp = ""
remove leading 1 if applicable
if len(strinput) = 11 and left(strinput, 1) = "1" then
strinput = right(strinput, 10)
end if
error catch to make sure strinput is proper length now that
weve finished manipulating it.
if not len(strinput) = 10 then
handle errors as you see fit. this script raises a real
error so you can handle it like any other runtime error,
but you could also pass an error back via the functions
return value or just display a message… your choice!
err.raise 1, "formatphonenumber function", _
"the phone number to be formatted must be a valid 10 digit us phone number!"
two alternative error techniques!
response.write "<b>the phone number to be formatted must be a valid phone number!</b>"
response.end
note if you use this youll also need to check for
this below so you dont overwrite it!
strtemp = "<b>the phone number to be formatted must be a valid phone number!</b>"
end if
if an error occurred then the rest of this wont get processed!
build the output string formatted to our liking!
(xxx) xxx-xxxx
strtemp = "(" "("
strtemp = strtemp & left(strinput, 3) area code
strtemp = strtemp & ") " ") "
strtemp = strtemp & mid(strinput, 4, 3) exchange
strtemp = strtemp & "-" "-"
strtemp = strtemp & right(strinput, 4) 4 digit part
set return value
formatphonenumber = strtemp
end function
***** end function area *****
%>
<% runtime code
dim strnumbertoformat the phone number we pass to the function
retrieve the requested number or set it to the default
if request.querystring("phone_number") <> "" then
strnumbertoformat = request.querystring("phone_number")
else
strnumbertoformat = "1-800-555-1212"
end if
we need to turn this on if we want to trap errors.
otherwise the script would generate an error if the input
number wasnt correct.
on error resume next
%>
<table border="1">
<tr>
<td>phone number before formatting:</td>
<td><%= strnumbertoformat %></td>
</tr>
<tr>
<td>phone number after formatting:</td>
<td>
<%
call the function and output the results
response.write formatphonenumber(strnumbertoformat)
check for an error and display the message if one occurred
if err.number then response.write err.description
%>
</td>
</tr>
</table>
<form action="39.asp" method="get">
phone number to format: <input type="text" name="phone_number" value="<%= strnumbertoformat %>">
<input type="submit" value="submit">
</form>
