Example of Restrict Decimals in ASP.NET MVC NumericTextBox Control
This sample demonstrates the decimal functionalities of the Numeric TextBox. Type a value in the input element to change dynamically, and it allows maximum of 3 decimal digits.
Numeric TextBox
Percentage TextBox
Currency TextBox
@Html.UiNumericTextBox("numerictextbox", new UiNumericTextBoxOptions { Value = 10, Format = "N3", Decimals = 3 })
@Html.UiNumericTextBox("percentage", new UiNumericTextBoxOptions { Format = "P3", Decimals = 3, Value = 0.5, Min = 0, Max = 1, Step = 0.01 })
@Html.UiNumericTextBox("currency", new UiNumericTextBoxOptions { Format = "C3", Decimals = 3, Value = 100 })
<style>
.content-wrapper { width: 90%; max-width: 420px; margin: 0 auto; min-width: 185px; }
.content-wrapper .control-label { padding: 24px 0 10px; font-size: 12px; }
.property-section .property-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 6px 0; }
.property-section .prop-input { width: 100px; 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>
<div class="control-section">
<div class="content-wrapper">
<div class="control-label">Numeric TextBox</div>
@Html.UiNumericTextBox("numerictextbox", new UiNumericTextBoxOptions { Value = 10, Format = "N3", Decimals = 3 })
<div class="control-label">Percentage TextBox</div>
@Html.UiNumericTextBox("percentage", new UiNumericTextBoxOptions { Format = "P3", Decimals = 3, Value = 0.5, Min = 0, Max = 1, Step = 0.01 })
<div class="control-label">Currency TextBox</div>
@Html.UiNumericTextBox("currency", new UiNumericTextBoxOptions { Format = "C3", Decimals = 3, Value = 100 })
</div>
</div>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class NumericTextBoxController : Controller
{
// The page is Views/NumericTextBox/RestrictDecimals.cshtml; it draws what ViewData holds.
public ActionResult RestrictDecimals()
{
return View();
}
}
}
@* 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 NumericTextBox provides an option to restrict the number of decimal values by using the decimals property. To restrict the number of decimal values on typing, use the decimals and validateDecimalOnType properties.