home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / ASP / phone_format.asp < prev    next >
Encoding:
Text File  |  2001-06-08  |  4.4 KB  |  132 lines

  1. <%
  2. '*******************************************************
  3. '*     ASP 101 Sample Code - http://www.asp101.com     *
  4. '*                                                     *
  5. '*   This code is made available as a service to our   *
  6. '*      visitors and is provided strictly for the      *
  7. '*               purpose of illustration.              *
  8. '*                                                     *
  9. '* Please direct all inquiries to webmaster@asp101.com *
  10. '*******************************************************
  11. %>
  12.  
  13. <%
  14. '***** BEGIN FUNCTION AREA *****
  15.  
  16. ' Formats a given 10 digit number into a nice looking phone number
  17. ' Example: given strNumber of 8005551212 you get (800) 555-1212
  18. Function FormatPhoneNumber(strNumber)
  19.     Dim strInput       ' String to hold our entered number
  20.     Dim strTemp        ' Temporary string to hold our working text
  21.     Dim strCurrentChar ' Var for storing each character for eval.
  22.     Dim I               ' Looping var
  23.     
  24.     ' Uppercase all characters for consistency
  25.     strInput = UCase(strNumber)
  26.  
  27.     ' To be able to handle some pretty bad formatting we strip out
  28.     ' all characters except for chars A to Z and digits 0 to 9
  29.     ' before proceeding.  I left in the chars for stupid slogan
  30.     ' numbers like 1-800-GET-CASH etc...
  31.     For I = 1 To Len(strInput)
  32.         strCurrentChar = Mid(strInput, I, 1)
  33.         ' Numbers (0 to 9)
  34.         If Asc("0") <= Asc(strCurrentChar) And Asc(strCurrentChar) <= Asc("9") Then
  35.             strTemp = strTemp & strCurrentChar
  36.         End If 
  37.         ' Upper Case Chars (A to Z)
  38.         If Asc("A") <= Asc(strCurrentChar) And Asc(strCurrentChar) <= Asc("Z") Then
  39.             strTemp = strTemp & strCurrentChar
  40.         End If 
  41.     Next 'I
  42.     
  43.     ' Swap strTemp back to strInput for next set of validation
  44.     ' I also clear strTemp just for good measure!
  45.     strInput = strTemp
  46.     strTemp = ""
  47.     
  48.     ' Remove leading 1 if applicable
  49.     If Len(strInput) = 11 And Left(strInput, 1) = "1" Then
  50.         strInput = Right(strInput, 10)
  51.     End If
  52.     
  53.     ' Error catch to make sure strInput is proper length now that
  54.     ' we've finished manipulating it.
  55.     If Not Len(strInput) = 10 Then
  56.         ' Handle errors as you see fit.  This script raises a real
  57.         ' error so you can handle it like any other runtime error,
  58.         ' but you could also pass an error back via the function's
  59.         ' return value or just display a message... your choice!
  60.         Err.Raise 1, "FormatPhoneNumber function", _
  61.             "The phone number to be formatted must be a valid 10 digit US phone number!"
  62.  
  63.         ' Two alternative error techniques!
  64.         'Response.Write "<B>The phone number to be formatted must be a valid phone number!</B>"
  65.         'Response.End
  66.  
  67.         ' Note if you use this you'll also need to check for
  68.         ' this below so you don't overwrite it!
  69.         'strTemp = "<B>The phone number to be formatted must be a valid phone number!</B>"
  70.     End If
  71.     
  72.     ' If an error occurred then the rest of this won't get processed!
  73.  
  74.     ' Build the output string formatted to our liking!
  75.     ' (xxx) xxx-xxxx
  76.     strTemp = "("                             ' "("
  77.     strTemp = strTemp & Left(strInput, 3)     ' Area code
  78.     strTemp = strTemp & ") "                  ' ") "
  79.     strTemp = strTemp & Mid(strInput, 4, 3)   ' Exchange
  80.     strTemp = strTemp & "-"                   ' "-"
  81.     strTemp = strTemp & Right(strInput, 4)    ' 4 digit part
  82.  
  83.     ' Set return value
  84.     FormatPhoneNumber = strTemp
  85. End Function
  86.  
  87. '***** END FUNCTION AREA *****
  88. %>
  89.  
  90.  
  91. <%' Runtime Code
  92. Dim strNumberToFormat ' The phone number we pass to the function
  93.  
  94.  
  95. ' Retrieve the requested number or set it to the default
  96. If Request.QueryString("phone_number") <> "" Then
  97.     strNumberToFormat = Request.QueryString("phone_number")
  98. Else
  99.     strNumberToFormat = "1-800-555-1212"
  100. End If
  101.  
  102. ' We need to turn this on if we want to trap errors.
  103. ' Otherwise the script would generate an error if the input
  104. ' number wasn't correct.
  105. On Error Resume Next
  106. %>
  107.  
  108. <TABLE BORDER="1">
  109.     <TR>
  110.         <TD>Phone number before formatting:</TD>
  111.         <TD><%= strNumberToFormat %></TD>
  112.     </TR>
  113.     <TR>
  114.         <TD>Phone number after formatting:</TD>
  115.         <TD>
  116.         <%
  117.         ' Call the function and output the results
  118.         Response.Write FormatPhoneNumber(strNumberToFormat)
  119.         
  120.         ' Check for an error and display the message if one occurred
  121.         If Err.number Then Response.Write Err.description
  122.         %>
  123.         </TD>
  124.     </TR>
  125. </TABLE>
  126.  
  127. <FORM ACTION="phone_format.asp" METHOD="get">
  128.     Phone number to format: <INPUT TYPE="text" NAME="phone_number" VALUE="<%= strNumberToFormat %>">
  129.     <INPUT type="submit" value="Submit">
  130. </FORM>
  131.  
  132.