Click here to Skip to main content
15,883,901 members
Articles / Web Development / ASP.NET
Article

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.3K   555   19   17
TextBox Control for ASP.NET with masking facility
Screenshot - Mask1.jpg

Shot2

Introduction


As we all knows, there is a MaskedTextBox control for Windows Application, where as in web, there is no masking for TextBox is available, this is the solution of the maskof the text box.

It will allow user to insert the text in predefined mask only.

Background


Some companies like telerik are providing this control for $299 or above, eventhough we are having knowledge, why should I pay? This was the question which inspired me to create this control.

Using the code


A brief description of how to use the article or code. The class names, the methods and properties, any tricks or tips.

Blocks of code should be set as style "Formatted" like this:

<%@ Register Assembly="MaskingText" Namespace="MaskingText" TagPrefix="cc1" %>
<cc1:MaskedText ID="MaskedText1" runat="server" DefaultMask="Phone" Mask="(###) ###-####">(___) ___-____</cc1:MaskedText>

Below is the code syntax for web control library:

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
Description:


By just setting the mask property, user can easily use this control.

Things to be note:

1. # sign indicate the Numeric Character.

2. @ sign indicate the Alpha Character, where as

3. ? sign indicates any character.

For example, if user want to mask this textbox for the format "(123)-(ABC)-(1DE)"

the format string will be "(###)-(@@@)-(#@@)"

Very easy to use....!!

Isn't it???

Points of Interest


There are predefined mask also like IPAddress, Phone and SSN, which automatically format the text box.

Hope this will help we (Web Developer) to provide a goof UI.

Have a Happy Programming....!! :)

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

 
GeneralGood Control Pin
vvasani28-Jun-08 0:49
vvasani28-Jun-08 0:49 
Generala few problems Pin
TobyKraft28-Sep-07 4:51
TobyKraft28-Sep-07 4:51 
Paresh,
Very nice work and very useful but I found a couple of weaknesses...

The processText routine takes a bit of time and it's easy to type too fast for the routine to keep up and you lose characters. (tested on 1.6Ghz processor) Not sure what can be done to speed it up.

When you Tab into a masked textbox, the cursor is on the last character position, not the first.

When you exit an input area, you cannot backspace back to the prior section so the user has to use the mouse to force the cursor into a different area. (such as in a phone number after entering the area code you can't backspace back into it) very annoying for data entry work.

Suggestion - have the Home key go to the 1st position, have the End key go to the last position.

Thanks,




Toby Kraft

GeneralRe: a few problems Pin
Alan Vu16-Feb-10 11:25
Alan Vu16-Feb-10 11:25 
QuestionRe: a few problems Pin
Pavan202010-Mar-13 11:37
Pavan202010-Mar-13 11:37 
AnswerRe: a few problems Pin
Paresh Rathod10-Mar-13 21:16
Paresh Rathod10-Mar-13 21:16 
General.NET 1.1 Pin
Hobo Spider25-Sep-07 2:36
Hobo Spider25-Sep-07 2:36 
GeneralRe: .NET 1.1 Pin
Paresh Rathod25-Sep-07 20:07
Paresh Rathod25-Sep-07 20:07 
GeneralRe: .NET 1.1 Pin
Nick Kowalewicz27-Sep-07 23:00
Nick Kowalewicz27-Sep-07 23:00 
QuestionOne Query Pin
Jigar K Oza24-Sep-07 21:08
Jigar K Oza24-Sep-07 21:08 
AnswerRe: One Query Pin
Paresh Rathod25-Sep-07 0:02
Paresh Rathod25-Sep-07 0:02 
GeneralRe: One Query Pin
Jigar K Oza25-Sep-07 18:28
Jigar K Oza25-Sep-07 18:28 
GeneralRe: One Query Pin
Paresh Rathod25-Sep-07 20:06
Paresh Rathod25-Sep-07 20:06 
GeneralGreat Pin
merlin98121-Sep-07 3:54
professionalmerlin98121-Sep-07 3:54 
GeneralRe: Great Pin
Paresh Rathod21-Sep-07 4:57
Paresh Rathod21-Sep-07 4:57 
Generalinteresting.. thanks.. Pin
Michael Sync20-Sep-07 20:37
Michael Sync20-Sep-07 20:37 
GeneralRe: interesting.. thanks.. Pin
Paresh Rathod21-Sep-07 0:16
Paresh Rathod21-Sep-07 0:16 
GeneralRe: interesting.. thanks.. Pin
Michael Sync22-Sep-07 18:46
Michael Sync22-Sep-07 18:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.