用ASP实现号码转换

 1   
 2'***** BEGIN FUNCTION AREA *****   
 3  
 4' Formats a given 10 digit number into a nice looking phone number   
 5' Example: given strNumber of 8005551212 you get (800) 555-1212   
 6Function FormatPhoneNumber(strNumber)   
 7Dim strInput ' String to hold our entered number   
 8Dim strTemp ' Temporary string to hold our working text   
 9Dim strCurrentChar ' Var for storing each character for eval.   
10Dim I ' Looping var   
11  
12' Uppercase all characters for consistency   
13strInput = UCase(strNumber)   
14  
15' To be able to handle some pretty bad formatting we strip out   
16' all characters except for chars A to Z and digits 0 to 9   
17' before proceeding. I left in the chars for stupid slogan   
18' numbers like 1-800-GET-CASH etc...   
19For I = 1 To Len(strInput)   
20strCurrentChar = Mid(strInput, I, 1)   
21' Numbers (0 to 9)   
22If Asc("0") <= Asc(strCurrentChar) And Asc(strCurrentChar) <= Asc("9") Then   
23strTemp = strTemp & strCurrentChar   
24End If   
25' Upper Case Chars (A to Z)   
26If Asc("A") <= Asc(strCurrentChar) And Asc(strCurrentChar) <= Asc("Z") Then   
27strTemp = strTemp & strCurrentChar   
28End If   
29Next 'I   
30  
31' Swap strTemp back to strInput for next set of validation   
32' I also clear strTemp just for good measure!   
33strInput = strTemp   
34strTemp = ""   
35  
36' Remove leading 1 if applicable   
37If Len(strInput) = 11 And Left(strInput, 1) = "1" Then   
38strInput = Right(strInput, 10)   
39End If   
40  
41' Error catch to make sure strInput is proper length now that   
42' we've finished manipulating it.   
43If Not Len(strInput) = 10 Then   
44' Handle errors as you see fit. This script raises a real   
45' error so you can handle it like any other runtime error,   
46' but you could also pass an error back via the function's   
47' return value or just display a message... your choice!   
48Err.Raise 1, "FormatPhoneNumber function", _   
49"The phone number to be formatted must be a valid 10 digit US phone number!"   
50  
51' Two alternative error techniques!   
52'Response.Write "

<b>The phone number to be formatted must be a valid phone number!</b>

1"   
2'Response.End   
3  
4' Note if you use this you'll also need to check for   
5' this below so you don't overwrite it!   
6'strTemp = "

<b>The phone number to be formatted must be a valid phone number!</b>

 1"   
 2End If   
 3  
 4' If an error occurred then the rest of this won't get processed!   
 5  
 6' Build the output string formatted to our liking!   
 7' (xxx) xxx-xxxx   
 8strTemp = "(" ' "("   
 9strTemp = strTemp & Left(strInput, 3) ' Area code   
10strTemp = strTemp & ") " ' ") "   
11strTemp = strTemp & Mid(strInput, 4, 3) ' Exchange   
12strTemp = strTemp & "-" ' "-"   
13strTemp = strTemp & Right(strInput, 4) ' 4 digit part   
14  
15' Set return value   
16FormatPhoneNumber = strTemp   
17End Function   
18  
19'***** END FUNCTION AREA *****   
 1' Runtime Code   
 2Dim strNumberToFormat ' The phone number we pass to the function   
 3  
 4  
 5' Retrieve the requested number or set it to the default   
 6If Request.QueryString("phone_number") <> "" Then   
 7strNumberToFormat = Request.QueryString("phone_number")   
 8Else   
 9strNumberToFormat = "1-800-555-1212"   
10End If   
11  
12' We need to turn this on if we want to trap errors.   
13' Otherwise the script would generate an error if the input   
14' number wasn't correct.   
15On Error Resume Next   
 1<table border="1">
 2<tr>
 3<td>Phone number before formatting:</td>
 4<td>```
 5= strNumberToFormat 
 6```</td>
 7</tr>
 8<tr>
 9<td>Phone number after formatting:</td>
10<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

1</td>
2</tr>
3</table>
1<form action="39.asp" method="get">   
2Phone number to format: <input name="phone_number" type="text" value="```
3= strNumberToFormat 
4```"/>
5<input type="submit" value="Submit"/>
6</form>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus