Getting Started with the MaskedTextBox
This tutorial explains how to set up a basic Telerik UI for ASP.NET Core 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. Finally, you can run the sample code in Telerik REPL and continue exploring the components.
Prerequisites
To successfully complete the tutorial, you need a project that is already configured to use the Telerik UI for ASP.NET Core components:
You can use the Telerik REPL playground and skip installing the components on your system and configuring a project.
-
You can prepare a Visual Studio project by following the following guide:
- Creating a new pre-configured project for the Telerik UI for ASP.NET Core components from a project template.
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 Core HtmlHelpers:
@using Kendo.Mvc.UI
-
To use the Telerik UI for ASP.NET Core TagHelpers:
@addTagHelper *, Kendo.Mvc
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>
@addTagHelper *, Kendo.Mvc
<h4>MaskedTextBox</h4>
<div>
</div>
2. Initialize the MaskedTextBox
Use the MaskedTextBox HtmlHelper or TagHelper to add the component to a page:
- The
Name()
configuration method is mandatory as its value is used for theid
and thename
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>
@addTagHelper *, Kendo.Mvc
<h4>MaskedTextBox</h4>
<div>
<kendo-maskedtextbox name="maskedtextbox" style="width: 300px;"
mask="(000) 000-0000">
</kendo-maskedtextbox>
</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>
@addTagHelper *, Kendo.Mvc
<h4>MaskedTextBox</h4>
<div>
<kendo-maskedtextbox name="maskedtextbox" style="width: 300px;"
mask="(000) 000-0000">
<maskedtextbox-label content="Phone Number:"/>
</kendo-maskedtextbox>
</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>
@addTagHelper *, Kendo.Mvc
<h4>MaskedTextBox</h4>
<div>
<script>
function change(e) {
console.log("Change :: " + this.value());
}
</script>
<kendo-maskedtextbox name="maskedtextbox" style="width: 300px;"
mask="(000) 000-0000" on-change="change">
<maskedtextbox-label content="Phone Number:"/>
</kendo-maskedtextbox>
</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:
-
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>
-
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>
Explore this Tutorial in REPL
You can continue experimenting with the code sample above by running it in the Telerik REPL server playground: