Example of Formats in ASP.NET MVC MaskedTextBox Control
This sample demonstrates the different formats that can be applied to MaskedTextBox component. You can customize the mask and prompt character values in the property panel.
Formats
Mask
Prompt Character
Value
Masked Value
@Html.UiMaskedTextBox("mask1", new UiMaskedTextBoxOptions { Mask = "(999)-999-9999", OnInput = "OnChange", OnChange = "OnChange" })
@Html.UiDropDownList("ddl", new UiDropDownListOptions { Width = "100px", Value = "_", OnChange = "valueChange", Items = ((IEnumerable<InputsDemoController.GameList>)ViewData["data"]).Select(g => new UiListItem(g.Id, g.Mask)).ToList() })
<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
<div class="control-section">
<div class="content-wrapper">
<div class="control-label">Formats</div>
@Html.UiMaskedTextBox("mask1", new UiMaskedTextBoxOptions { Mask = "(999)-999-9999", OnInput = "OnChange", OnChange = "OnChange" })
</div>
</div>
<div class="demo-property-panel property-section" title="Properties">
<div class="property-row"><span>Mask</span> <input id="input1" type="text" class="prop-input" value="(999)-999-9999" /></div>
<div class="property-row"><span>Prompt Character</span> @Html.UiDropDownList("ddl", new UiDropDownListOptions { Width = "100px", Value = "_", OnChange = "valueChange", Items = ((IEnumerable<InputsDemoController.GameList>)ViewData["data"]).Select(g => new UiListItem(g.Id, g.Mask)).ToList() })</div>
<div class="property-row"><span>Value</span> <span id="val1"></span></div>
<div class="property-row"><span>Masked Value</span> <span id="val2"></span></div>
</div>
<script>
// Syncfusion: the panel changes the mask and the prompt character; the value and the masked value follow the typing
var ele1 = document.getElementById('input1');
var ele3 = document.getElementById('val1');
var ele4 = document.getElementById('val2');
function mask() { return window.Lumen.get('mask1'); }
function show() { ele3.textContent = mask().getValue(); ele4.textContent = mask().getMaskedValue(); }
function valueChange(value) { if (value) { mask().setPromptChar(value); show(); } }
function OnChange() { show(); }
ele1.oninput = function () { mask().setMask(ele1.value); show(); };
window.addEventListener('load', function () { show(); });
</script>
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/Formats.cshtml; it draws what ViewData holds.
public ActionResult Formats()
{
ViewData["data"] = new GameList().MaskEditModel();
return View();
}
public class GameList
{
public string Id { get; set; }
public string Mask { get; set; }
public List<GameList> MaskEditModel()
{
List<GameList> game = new List<GameList>();
game.Add(new GameList { Id = "#", Mask = "#" });
game.Add(new GameList { Id = "_", Mask = "_" });
game.Add(new GameList { Id = "*", Mask = "*" });
game.Add(new GameList { Id = "@", Mask = "@" });
return game;
}
}
}
}
@* 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>
Here, the "Value" and "Masked Value" labels from the properties panel returns the raw value (unmasked value) and masked value of the MaskedTextBox component. You can also get these raw value and masked value anytime through the value property and getMaskedValue method.