Search results

Getting Started

This section briefly explains about how to include a simple NumericTextBox in your ASP.NET MVC application. You can refer ASP.NET MVC Getting Started documentation page for introduction part part of the system requirements and configure the common specifications.

Adding component to the Application

NumericTextBox component can be rendered by using the EJS().NumericTextBox() tag helper in ASP.NET MVC application. Add the below simple code to your index.cshtml page which is available within the Views/Home folder, to initialize the NumericTextBox.

The following example shows a basic NumericTextBox component.

razor
demo.cs
@section ControlsSection{

 @Html.EJS().NumericTextBox("numeric").Value(10).Render()
           
}
public ActionResult Demo()
{
    return View();
}

Output be like the below.

NumericTextBox Sample

Range validation

You can set the minimum and maximum range of values in the NumericTextBox using the min and max properties, so the numeric value should be in the min and max range.

The validation behavior depends on the strictMode property.

The below example demonstrates range validation.

razor
range.cs
@section ControlsSection{
       @Html.EJS().NumericTextBox("numeric").Value(16).Min(10).Max(20).Step(2).Render()
}
public ActionResult Range()
{
    return View();
}

Formatting the value

User can set the format of the NumericTextBox component using format property. The value will be displayed in the specified format, when the component is in focused out state. For more information about formatting the value, refer to this link.

The below example demonstrates format the value by using currency format value c2.

razor
format.cs
@section ControlsSection{

  @Html.EJS().NumericTextBox("numeric").Format("c2").Value(10).Render()
}
public ActionResult Format()
{
    return View();
}

Output be like the below.

NumericTextBox Sample

Precision of numbers

You can restrict the number of decimals to be entered in the NumericTextBox by using the decimals and validateDecimalOnType properties. So, you can’t enter the number whose precision is greater than the mentioned decimals.

  • If validateDecimalOnType is false, number of decimals will not be restricted. Else, number of decimals will be restricted while typing in the NumericTextBox.
razor
precision.cs
@section ControlsSection{
            <div id='container'>
                <div class='wrap'>
                    @Html.EJS().NumericTextBox("strict").Format("n3").Value(10).ValidateDecimalOnType(true).Decimals(3).Placeholder("ValidateDecimalOnType enabled").FloatLabelType(FloatLabelType.Auto).Render()
                </div>
                <div class='wrap'>
                    @Html.EJS().NumericTextBox("allow").Format("n3").Value(10).Decimals(3).Placeholder("ValidateDecimalOnType enabled").FloatLabelType(FloatLabelType.Auto).Render()
                </div>
            </div>
}
public ActionResult Precision()
{
    return View();
}

Output be like the below.

NumericTextBox Sample

See Also