ASP 101 - Active Server Pages 101 phone_format.aspx
<%@ Page Language="VB" %>
<script language="VB" runat="server">
	Sub Page_Load(sender as Object, e as EventArgs)
		' Put in a dummy phone number and run the function once
		' on first load so people have something to see when they
		' arrive.
		If Not Page.IsPostBack Then
			lblPhoneNumberBefore.Text = "1-800-555-1212"
			lblPhoneNumberAfter.Text  = FormatPhoneNumber("1-800-555-1212")
		End If
	End Sub
	Sub btnFormatPhoneNumber_OnClick(sender as Object, e as EventArgs)
		' Take input from TextBox and echo it out to our first Label control.
		lblPhoneNumberBefore.Text = txtPhoneNumber.Text
		' Take input from TextBox and run it through our formatting
		' function and display the result via the second Label control.
		lblPhoneNumberAfter.Text  = FormatPhoneNumber(txtPhoneNumber.Text)
	End Sub
	' 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 As String) As String
		Dim strInput       As String  ' String to hold our entered number
		Dim strTemp        As String  ' Temporary string to hold our working text
		Dim strCurrentChar As String  ' Var for storing each character for eval.
		Dim I	           As Integer ' 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.  You can certainly remove
		' them if you don't want to allow their use.
		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
		' we've finished manipulating it.  You can allow longer numbers
		' if you prefer... just being strict to illustrate the error
		' handling.
		Try
			If Not Len(strInput) = 10 Then
				' Handle errors as you see fit.  This script raises a real
				' error for illustration, but if you prefer you can just
				' pass the error message back via the function's return
				' value since that's all I end up doing anyway!
				Err.Raise(1, "FormatPhoneNumber function", _
					"The phone number to be formatted must be a valid 10 digit US phone number!")
			End If
			' 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
		Catch ex As Exception
			'strTemp = ex.ToString()
			strTemp = "The phone number to be formatted must be a valid 10 digit US phone number!"
			Server.ClearError()
		End Try
		' Set return value
		FormatPhoneNumber = strTemp
	End Function
</script>
<html>
<head>
<title>ASP.NET Phone Number Formatting Sample</title>
</head>
<body>
<form runat="server">
<table border="1">
	<tr>
		<td>Phone number before formatting:</td>
		<td><asp:Label id="lblPhoneNumberBefore" runat="server" /></td>
	</tr>
	<tr>
		<td>Phone number after formatting:</td>
		<td><asp:Label id="lblPhoneNumberAfter" runat="server" /></td>
	</tr>
</table>
<p>
Phone number to format:
<asp:TextBox id="txtPhoneNumber" runat="server" />
<asp:Button id="btnFormatPhoneNumber" runat="server"
	OnClick = "btnFormatPhoneNumber_OnClick"
	Text    = "Submit"
/>
</p>
</form>
<hr />
<p>
Click <a href="http://www.asp101.com/samples/phone_format_aspx.asp">here</a>
to read about and download the source code.
</p>
</body>
</html>