Validation
The MaskedTextBox provides a set of predefined mask rules and enables you to modify them.
If no mask is defined, the MaskedTextBox allows for any input.
Predefined Masks
The MaskedTextBox has a list of predefined mask rules which can be used to validate its user input.
The following example demonstrates how to set a zip code
mask.
@(Html.Kendo().MaskedTextBox()
.Name("maskedtextbox")
.Mask("00000-9999") // Set the zip code.
)
<kendo-maskedtextbox name="maskedtextbox" mask="00000-9999"></kendo-maskedtextbox>
Custom Masks
The MaskedTextBox enables you to define custom mask rules if none of the predefined ones is suitable. To add a custom rule, use the Rules
method.
The MaskedTextBox supports JavaScript Regular Expressions defined as a string or a JavaScript function.
The following example demonstrates how to define a custom rule for the -
(minus) and +
(plus) symbols.
@(Html.Kendo().MaskedTextBox()
.Name("maskedtextbox")
.Rules(rules => {
rules.Add('~', "/[+-]/");
})
.Mask("~0000") // Set a mask with a custom rule.
)
@{
var rules = new Dictionary<string, string>()
{
{"~", "/[+-]/"}
};
}
<kendo-maskedtextbox name="maskedtextbox" rules="@rules" mask="~0000" ></kendo-maskedtextbox>