' Formats a given digit a nice looking phone number
' Example:given Strnumber of 8005551212 for You (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 is 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) = one and left (strinput, 1) = "1" Then
Strinput = Right (strinput, 10)
End If
' Error catch to make sure strinput is proper length
' We ' ve finished manipulating it.
If not Len (strinput) = Then
' Handle errors as you are fit. This script raises a real
' Error so can handle it like any other runtime error,
' But you could also pass an error back via the function ' s
' Return value or just display a message ... your choice!
Err.Raise 1, "Formatphonenumber function", _
"The phone number to is formatted must be a valid digit US phone number!"
' Two alternative error techniques!
' Response.Write ' <b>the phone number to is formatted must be a valid phone number!</b> "
' Response.End
' If you'd also need to check for
' This is below so don ' t overwrite it!
' strtemp = ' <b>the phone number to is formatted must be a valid phone number!</b> '
End If
' If an ' error occurred then the rest of this won ' t 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 the ' if we want to trap errors.
' Otherwise the script would generate an error if the input
' Number wasn ' t 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 ' function and output the results
Response.Write Formatphonenumber (Strnumbertoformat)
' Check for an error and display ' If one occurred
If Err.Number Then Response.Write Err.Description
%>
</TD>
</TR>
</TABLE>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service