Example of Custom Mask in ASP.NET MVC MaskedTextBox Control
This sample demonstrates the custom mask functionalities of MaskedTextBox component. Enter time value in meridiem format, and enter IP value in numeric format.
@Html.UiMaskedTextBox("mask1", new UiMaskedTextBoxOptions { Mask = "00:00 >PM", CustomCharacters = new Dictionary<string, string> { { "P", custom.P }, { "M", custom.M } } })
@Html.UiMaskedTextBox("mask2", new UiMaskedTextBoxOptions { Mask = "[0-2][0-9][0-9].[0-2][0-9][0-9].[0-2][0-9][0-9].[0-2][0-9][0-9]" })
<style>
.content-wrapper { width: 90%; max-width: 400px; margin: 0 auto; min-width: 185px; }
.content-wrapper .control-label { padding: 24px 0 4px; font-size: 12px; opacity: 0.7; }
.property-section .property-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 6px 0; }
.property-section .prop-input { width: 150px; padding: 4px 6px; border: 1px solid var(--ui-input-border); border-radius: var(--ui-radius); background: var(--ui-surface); color: var(--ui-text); font: inherit; }
</style>
@using Lumen.UI.Mvc.Demo.Controllers
@{ var custom = (InputsDemoController.CustomCharacters)ViewData["cusObj"]; }
<div class="control-section">
<div class="content-wrapper">
<div class="control-label">Time (ex: 10:00 PM, 10:00 AM)</div>
@Html.UiMaskedTextBox("mask1", new UiMaskedTextBoxOptions { Mask = "00:00 >PM", CustomCharacters = new Dictionary<string, string> { { "P", custom.P }, { "M", custom.M } } })
<div class="control-label">IP Address (ex: 212.212.111.222)</div>
@Html.UiMaskedTextBox("mask2", new UiMaskedTextBoxOptions { Mask = "[0-2][0-9][0-9].[0-2][0-9][0-9].[0-2][0-9][0-9].[0-2][0-9][0-9]" })
</div>
</div>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class MaskedTextBoxController : Controller
{
// The page is Views/MaskedTextBox/CustomMask.cshtml; it draws what ViewData holds.
public ActionResult CustomMask()
{
CustomCharacters customObj = new CustomCharacters();
customObj.P = "P,A,a,p";
customObj.M = "M,m";
ViewData["cusObj"] = customObj;
return View();
}
public class CustomCharacters
{
public string P { get; set; }
public string M { get; set; }
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>
The custom mask is achieved by the following ways:
You can form the mask with any non-mask elements (literals), and you can configure the behavior of that non-mask element as mask element through the customCharacters property.
In this demo, the “Time” mask '99 : 99 >PM' contains the literals P and M, where it is configured to allow the inputs as 'P', 'A', 'p', 'a', and 'M', 'm'.
Alternatively, you can use the regular expressions as mask element to validate the input of the particular input place.
Here, in the “IP Address” example, each character is masked by an regular expression to allow a particular range of values.