New to Telerik UI for ASP.NET MVC? Download free 30-day trial

Getting Started with the MaskedTextBox

This tutorial explains how to set up a basic Telerik UI for ASP.NET MVC MaskedTextBox and highlights the major steps in the configuration of the component.

You will initialize a MaskedTextBox component. Then you'll configure the mask and the label for the input.

Sample Telerik UI for ASP.NET MVC MaskedTextBox

Prerequisites

To successfully complete the tutorial, you need a project that is already configured to use the Telerik UI for ASP.NET MVC components:

1. Prepare the CSHTML File

The first step is to add the required directives at the top of the .cshtml document:

  • To use the Telerik UI for ASP.NET MVC HtmlHelpers:

    @using Kendo.Mvc.UI
    

Optionally, you can structure the document by adding the desired HTML elements like headings, divs, paragraphs, and others.

    @using Kendo.Mvc.UI

    <h4>MaskedTextBox</h4>
    <div>

    </div>

2. Initialize the MaskedTextBox

Use the MaskedTextBox HtmlHelper to add the component to a page:

  • The Name() configuration method is mandatory as its value is used for the id and the name attributes of the MaskedTextBox element.
  • The Mask() configuration specifies the input mask that constraints the format and the type of characters entered by the user.
@using Kendo.Mvc.UI

<h4>MaskedTextBox</h4>
<div>
    @(Html.Kendo().MaskedTextBox()
        .Name("maskedtextbox")
        .Mask("(000) 000-0000")
        .HtmlAttributes(new { style = "width: 300px;" })
    )
</div>

3. Define a Label Text

The next step is to present a description in front of the MaskedTextBox component by using the Label() property.

@using Kendo.Mvc.UI

<h4>MaskedTextBox</h4>
<div>
    @(Html.Kendo().MaskedTextBox()
        .Name("maskedtextbox")
        .Mask("(000) 000-0000")
        .Label(label=>label.Content("Phone Number:"))
        .HtmlAttributes(new { style = "width: 300px;" })
    )
</div>

4. Handle a MaskedTextBox Event

The MaskedTextBox component provides convenient events for implementing your desired logic. In this tutorial, you will use the exposed Change() event to log a new entry in the browser's console.

@using Kendo.Mvc.UI

<h4>MaskedTextBox</h4>
<div>
    <script>
       function change(e) {
          console.log("Change :: " + this.value());
       }
    </script>

    @(Html.Kendo().MaskedTextBox()
        .Name("maskedtextbox")
        .Mask("(000) 000-0000")
        .Label(l => l.Content("Phone Number:"))
        .Events(e => e.Change("change"))
        .HtmlAttributes(new { style = "width: 300px;" })
    )
</div>

5. (Optional) Reference Existing MaskedTextBox Instances

You can reference the MaskedTextBox instances that you have created and build on top of their existing configuration:

  1. Use the id attribute of the component instance to establish a reference.

    <script>
        var maskedtextboxReference = $("#maskedtextbox").data("kendoMaskedTextBox"); // maskedtextboxReference is a reference to the existing MaskedTextBox instance of the helper.
    </script>
    
  2. Use the MaskedTextBox client-side API to control the behavior of the widget. In this example, you will use the value method to select an item.

    <script>
        var maskedtextboxReference = $("#maskedtextbox").data("kendoMaskedTextBox"); // maskedtextboxReference is a reference to the existing MaskedTextBox instance of the helper.
        maskedtextboxReference.value("Sample text"); 
    </script>
    

Next Steps

See Also

In this article