Guiding Input with a Masked Text Box

Guiding Input with a Masked Text Box

A Masked Text Box is a type of text box that helps guide user input by enforcing a specific format. It is commonly used for input fields that require a specific pattern, such as phone numbers, dates, or social security numbers.

The AJAX Control Toolkit provides a MaskedEdit control that you can use to create masked text boxes easily.

Here’s an example of how to use the MaskedEdit control in ASP.NET:

First, make sure you have the AJAX Control Toolkit installed in your project. You can download it from the official website: https://ajaxcontroltoolkit.devexpress.com/.

In your ASP.NET page, add a ScriptManager control if you don’t already have one:

<asp:ScriptManager ID=”ScriptManager1″ runat=”server”></asp:ScriptManager>

Add a TextBox control to your page and set its ID and runat attributes:

<asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox>

Add a MaskedEditExtender control to the TextBox. This control provides the masking functionality. Set its ID, TargetControlID (the ID of the TextBox), and Mask properties. The Mask property determines the format of the input. For example, the following Mask value will format the input as a phone number: “(999) 999-9999”.

<ajaxToolkit:MaskedEditExtender ID=”MaskedEditExtender1″ runat=”server” TargetControlID=”TextBox1″

Mask=”(999) 999-9999″></ajaxToolkit:MaskedEditExtender>

You can also add a MaskedEditValidator control to validate the input. Set its ID, ControlToValidate (the ID of the TextBox), and InvalidValueMessage properties. For example:

<ajaxToolkit:MaskedEditValidator ID=”MaskedEditValidator1″ runat=”server” ControlToValidate=”TextBox1″

InvalidValueMessage=”Invalid phone number” ControlExtender=”MaskedEditExtender1″></ajaxToolkit:MaskedEditValidator>

This will validate that the input matches the specified format.

With these controls in place, users will be guided to enter data in the specified format. For example, if you set the Mask property to “(999) 999-9999”, the control will automatically add the parentheses and hyphen as the user types. If the user types an invalid character, the control will ignore it. If the user types an incomplete phone number, the control will show the missing digits in gray until the number is complete.

Apply for ASP.NET Certification Now!!

https://www.vskills.in/certification/certified-aspnet-programmer

Back to Tutorial

Share this post
[social_warfare]
Adding Style to a Watermark
Creating a Masked Input

Get industry recognized certification – Contact us

keyboard_arrow_up