Click here to Skip to main content
15,891,136 members
Articles / Web Development / ASP.NET

MaskingText for ASP.NET

Rate me:
Please Sign up or sign in to vote.
3.68/5 (15 votes)
20 Sep 2007CPOL1 min read 51.5K   555   19  
TextBox Control for ASP.NET with masking facility
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls


<DefaultProperty("Text"), ToolboxData("<{0}:MaskedText runat=server></{0}:MaskedText>")> _
Public Class MaskedText
    Inherits System.Web.UI.WebControls.TextBox
    '# - Numeric
    '@ - Alphas
    '? - Any Character
    Private MaskingChars As New List(Of String)(New String() {"#", "?", "@"})
    Private DefaultMasks As New ArrayList(New Object() {"", "###.###.#.###", "(###) ###-####", "###-##-###"})
    Private _DefMask As CommonMasks
    Public Enum CommonMasks
        None = 0
        IPAddress = 1 '
        Phone = 2 '
        SSN = 3 '
    End Enum
    <Bindable(True), Category("Appearance"), DefaultValue(""), Localizable(True)> Public Overrides Property Text() As String
        Get
            Dim s As String = CStr(ViewState("Text"))
            If s Is Nothing Then
                Return String.Empty
            Else
                Return s
            End If
        End Get

        Set(ByVal Value As String)
            ViewState("Text") = Value
        End Set
    End Property
    <Bindable(True), Category("Mask"), DefaultValue(""), Localizable(True)> _
    Public Property DefaultMask() As CommonMasks
        Get
            Return _DefMask
        End Get
        Set(ByVal Value As CommonMasks)
            _DefMask = Value
            Me.Mask = CStr(DefaultMasks(DirectCast(_DefMask, Integer)))
        End Set
    End Property

    <Bindable(True), Category("Mask"), DefaultValue(""), Localizable(True)> _
    Public Property Mask() As String
        Get
            Dim s As String = CStr(ViewState("Mask"))
            If s Is Nothing Then
                Return String.Empty
            Else
                Return s
            End If
        End Get
        Set(ByVal Value As String)
            If Value <> "" Then
                Dim tempString As String = System.Text.RegularExpressions.Regex.Replace(Value, "\#|\?|\@", "_")
                Me.Text = tempString
                Me.MaxLength = Value.Length + 1
                ViewState("Mask") = Value
            End If
        End Set
    End Property

    Protected Overrides Sub RenderContents(ByVal output As HtmlTextWriter)
        output.Write(Text)
    End Sub

    Private Sub MaskedText_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        Me.Attributes.Add("onkeyup", "processText('" & Me.ClientID & "','" & Me.Mask & "')")
        Me.Attributes.Add("onkeydown", "return checkKey('" & Me.ClientID & "','" & Me.Mask & "')")
        Me.Page.ClientScript.RegisterClientScriptResource(GetType(MaskingText.MaskedText), "MaskingText.Mask.js")
    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
My self Paresh Rathod, working as a web developer in Rajkot, Guajrat, India.

I am having total experience of 10 yrs in this field.

I beleive in...
TOGETHER WE CAN AND WE WILL MAKE A DIFFERENCE.

Comments and Discussions