Edit this page

MaskedTextBox HtmlHelper Overview

The MaskedTextBox HtmlHelper extension is a server-side wrapper for the Kendo UI MaskedTextBox widget.

Configuration

Below are listed the steps for you to follow when configuring the Kendo UI MaskedTextBox.

  1. Make sure you followed all the steps from the introductory article on Telerik UI for ASP.NET MVC.

  2. Create a new action method which renders the view.

    Example
    public ActionResult Index()
    {
        return View();
    }
    
  3. Add a MaskedTextBox.

    Example
    
            <%: Html.Kendo().MaskedTextBox()
                    .Name("maskedtextbox") //The name of the MaskedTextBox is mandatory. It specifies the "id" attribute of the widget.
                    .Mask("(000) 000-0000") //Set the mask value of the MaskedTextBox.
                    .Value("(123) 345-6789") //Set the value of the MaskedTextBox.
            %>
    
    
            @(Html.Kendo().MaskedTextBox()
                  .Name("maskedtextbox") //The name of the MaskedTextBox is mandatory. It specifies the "id" attribute of the widget.
                  .Mask("(000) 000-0000") //Set the mask value of the MaskedTextBox.
                  .Value("(123) 345-6789") //Set the value of the MaskedTextBox.
            )
    

Define Mask Values

The MaskedTextBox has a list of predefined mask rules, which can be used to compose the mask of a widget.

The following example demonstrates how to set a zip code mask

Example

        <%: Html.Kendo().MaskedTextBox()
                .Name("maskedtextbox")
                .Mask("00000-9999") //Set the zip code.
        %>

        @(Html.Kendo().MaskedTextBox()
              .Name("maskedtextbox")
              .Mask("00000-9999") //Set the zip code.
        )

Important

If no mask is defined, the widget allows for any input.

Define Custom Mask Rules

The MaskedTextBox enables you to define custom mask rules if none of the predefined ones is sufficient. To add a custom rule, use the Rules method.

The following example demonstrates how to define a custom rule for the - (minus) and + (plus) symbols.

Example

      <%: Html.Kendo().MaskedTextBox()
              .Name("maskedtextbox")
              .Rules(rules => {
                  rules.Add('~', "/[+-]/");
              })
              .Mask("~0000") //Set a mask with a custom rule.
      %>

      @(Html.Kendo().MaskedTextBox()
            .Name("maskedtextbox")
            .Rules(rules => {
                rules.Add('~', "/[+-]/");
            })
            .Mask("~0000") //Set a mask with a custom rule.
      )

Important

Widgets supports JavaScript Regular Expressions defined as a string or a JavaScript function.

Event Handling

You can subscribe to all MaskedTextBox events.

By Handler Name

The following example demonstrates how to subscribe to events by a handler name.

Example

      <%: Html.Kendo().MaskedTextBox()
              .Name("maskedtextbox")
              .Events(e => e
                  .Change("maskedtextbox_change")
              )
      %>
      <script>
      function maskedtextbox_change() {
          //Handle the change event.
      }
      </script>

      @(Html.Kendo().MaskedTextBox()
            .Name("maskedtextbox")
            .Events(e => e
                .Change("maskedtextbox_change")
            )
      )
      <script>
      function maskedtextbox_change() {
          //Handle the change event.
      }
      </script>

By Template Delegate

The following example demonstrates how to subscribe to events by a template delegate.

Example

      @(Html.Kendo().MaskedTextBox()
            .Name("maskedtextbox")
            .Events(e => e
                .Change(@<text>
                  function() {
                      //Handle the change event inline.
                  }
                </text>)
            )
      )

Reference

Existing Instances

To reference an existing Kendo UI MaskedTextBox instance, use the jQuery.data() configuration option. Once a reference is established, use the MaskedTextBox API to control its behavior.

Example
  //Put this after your Kendo UI MaskedTextBox for ASP.NET MVC declaration.
  <script>
  $(function() {
      //Notice that the Name() of the MaskedTextBox is used to get its client-side instance.
      var maskedtextbox = $("#maskedtextbox").data("kendoMaskedTextBox");
  });
  </script>

See Also