# 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](https://www.telerik.com/aspnet-mvc/documentation/assets/dd06b138c65bdefdc56348fb94f2e6a3/maskedtextbox-getting-started.png)

## Prerequisites

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

* To create a new pre-configured project for the Telerik UI for ASP.NET MVC components, you can [use a project template](https://www.telerik.com/aspnet-mvc/documentation/getting-started-mvc/first-steps).

* To manually configure an existing project by using NuGet, see the [Adding Telerik UI through NuGet](https://www.telerik.com/aspnet-mvc/documentation/getting-started/setup-with-nuget).

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

  ```cshtml
  @using Kendo.Mvc.UI
  ```

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

```Razor HtmlHelper
    @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.

```Razor HtmlHelper
@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.

```Razor HtmlHelper
@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.

```Razor HtmlHelper
@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.

   ```JS script
   <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](https://docs.telerik.com/kendo-ui/api/ui/maskedtextbox#methods) to control the behavior of the widget. In this example, you will use the `value` method to select an item.

   ```JS script
   <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

* [Define Mask Rules](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/editors/maskedtextbox/mask-rules)
* [Set Labels to the MaskedTextBox](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/editors/maskedtextbox/labels)
* [Customize the Appearance of the MaskedTextBox](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/editors/maskedtextbox/appearance)

## See Also

* [Using the API of the MaskedTextBox for ASP.NET MVC (Demo)](https://demos.telerik.com/aspnet-mvc/maskedtextbox/api)
* [Client-Side API of the MaskedTextBox](https://docs.telerik.com/kendo-ui/api/ui/maskedtextbox)
* [Server-Side API of the MaskedTextBox](https://www.telerik.com/aspnet-mvc/documentation/api/maskedtextbox)
* [Knowledge Base Section](https://www.telerik.com/aspnet-mvc/documentation/knowledge-base)
