<%@Control Language="VB" Debug="True" %> <asp:TextBox id="txtMaskEdit" Columns="25" runat="server" /> <asp:RequiredFieldValidator id="valRequired" runat="server" ControlToValidate="txtMaskEdit" ErrorMessage="* You must enter a value" Display="dynamic"> * </asp:RequiredFieldValidator> <asp:RegularExpressionValidator id="valRegex" runat="server" ControlToValidate="txtMaskEdit" ErrorMessage="* Your entry does not match the mask" Display="dynamic"> * </asp:RegularExpressionValidator> <asp:ValidationSummary id="valSummary" runat="server" HeaderText="<b>The following errors were found:</b>" ShowSummary="true" DisplayMode="List" /> <%-------------------------------------------------------------%> <script runat="server"> <%-------------- Private Internal Variable -------------------%> Private _font As String = "Courier New" <%-------------- Public Property Variables -------------------%> Public Mask As String Public FontSize As Integer <%-------------- Page Load Event Handler ---------------------%> Sub Page_Load() ' add style attributes to Textbox txtMaskEdit.Style("font-family") = _font txtMaskEdit.Style("font-size") = FontSize & "pt" ' create mask for display as Textbox background Dim sQuery As String = Mask sQuery = sQuery.Replace("a", "_") sQuery = sQuery.Replace("A", "_") sQuery = sQuery.Replace("l", "_") sQuery = sQuery.Replace("L", "_") sQuery = sQuery.Replace("n", "_") sQuery = sQuery.Replace("?", "_") ' encode it for query string to pass to page ' mask-image.aspx that generates the image sQuery = Server.UrlEncode(sQuery) _font = Server.UrlEncode(_font) ' create and add background style attribute txtMaskEdit.Style("background-image") = "url(mask-image.aspx?mask=" _ & sQuery & "&font=" & _font & "&size=" & FontSize & "&cols=" _ & txtMaskEdit.Columns.ToString() & ")" ' create string to use as Tooltip for control Dim sTip As String = Mask sTip = sTip.Replace("a", "[a]") sTip = sTip.Replace("A", "[A]") sTip = sTip.Replace("l", "[l]") sTip = sTip.Replace("L", "[L]") sTip = sTip.Replace("n", "[n]") sTip = sTip.Replace("?", "[?]") txtMaskEdit.ToolTip = "Mask: " & sTip & vbCrlf & " where:" _ & vbCrlf & "[a] = any alphanumeric character (0-9, A-Z, a-z)" _ & vbCrlf & "[A] = an uppercase alphanumeric character (0-9, A-Z)" _ & vbCrlf & "[l] = any letter character (A-Z, a-z)" _ & vbCrlf & "[L] = an uppercase letter character (A-Z)" _ & vbCrlf & "[n] = any numeric character (0-9)" _ & vbCrlf & "[?] = any character" ' create regular expression for validation control Dim sRegex As String = Mask sRegex = sRegex.Replace("\", "\\") sRegex = sRegex.Replace("a", "[A-Z,a-z,0-9]") sRegex = sRegex.Replace("A", "[A-Z,0-9]") sRegex = sRegex.Replace("l", "[A-Z,a-z]") sRegex = sRegex.Replace("L", "[A-Z]") sRegex = sRegex.Replace("n", "[0-9]") sRegex = sRegex.Replace("?", ".") valRegex.ValidationExpression = sRegex ' add client-side event handler attributes txtMaskEdit.Attributes.Add("onkeydown", "return doKeyDown(event, this, '" & Mask & "')") txtMaskEdit.Attributes.Add("onkeypress", "return doKeyPress(event, this, '" & Mask & "')") txtMaskEdit.Attributes.Add("onkeyup", "return doKeyUp(event, this, '" & Mask & "')") txtMaskEdit.Attributes.Add("onfocus", "return doFocus(event, this, '" & Mask & "')") ' see if previous instance of this control has already ' added the required JavaScript code reference to the page If Not Page.IsClientScriptBlockRegistered("AHHMaskEdit") Then Dim sPath As String = "/aspnet_client/custom/" Dim sScript As String = "<script language='javascript' " _ & "src='" & sPath & "maskedit.js'><" & "/script>" ' add this JavaScript code to the page Page.RegisterClientScriptBlock("AHHMaskEdit", sScript) End If End Sub </script> <%-------------------------------------------------------------%>