Getting Started with ASP.NET Core Numeric Textbox Control

23 Jul 20264 minutes to read

This section briefly explains how to include the ASP.NET Core Numeric Textbox control in your ASP.NET Core application using Visual Studio.

Create an ASP.NET Core Web App with Razor pages

Create an ASP.NET Core Web App using Visual Studio via Microsoft Templates or the Syncfusion® ASP.NET Core Extension. For detailed instructions, refer to the ASP.NET Core Web App Getting Started documentation.

Install the required ASP.NET Core packages

To add ASP.NET Core Numeric Textbox control in the app, open the NuGet package manager in Visual Studio (Tools → NuGet Package Manager → Manage NuGet Packages for Solution), search for and install the Syncfusion.AspNetCore.Inputs and Syncfusion.AspNetCore.Themes packages. All Syncfusion ASP.NET Core packages are available on nuget.org. See the NuGet packages topic for details.

Alternatively, you can install the same packages using the Package Manager Console with the following command.

Install-Package Syncfusion.AspNetCore.Inputs -Version 34.1.29
Install-Package Syncfusion.AspNetCore.Themes -Version 34.1.29

Add ASP.NET Core Tag Helpers

After the packages are installed, open the ~/Pages/_ViewImports.cshtml file and import the Syncfusion.AspNetCore.Base and Syncfusion.AspNetCore.Inputs Tag Helpers.

@addTagHelper *, Syncfusion.AspNetCore.Base
@addTagHelper *, Syncfusion.AspNetCore.Inputs

Add stylesheet and script resources

The theme stylesheet and script can be referenced from CDN. Include the stylesheet and script references inside the <head> of ~/Pages/Shared/_Layout.cshtml file.

<head>
    ...
    @* ASP.NET Core controls styles *@
    <link rel="stylesheet" href="_content/Syncfusion.AspNetCore.Themes/styles/fluent2.css" />
    @* ASP.NET Core controls scripts *@
    <script src="_content/Syncfusion.AspNetCore.Inputs/scripts/sf-numerictextbox.min.js"></script>
</head>

Register the Script Manager

Open the ~/Pages/Shared/_Layout.cshtml file and register the script manager <ejs-scripts> at the end of the <body> element as follows.

<body>
    ...
    @* ASP.NET Core Script Manager *@
    <ejs-scripts></ejs-scripts>
</body>

Add ASP.NET Core Numeric Textbox control

Add the ASP.NET Core Numeric Textbox control in the ~/Pages/Index.cshtml file.

<ejs-numerictextbox id="numeric" value="10" Type="text"></ejs-numerictextbox>

Run the application

Press Ctrl+F5 (Windows) or +F5 (macOS) to launch the application. The ASP.NET Core Numeric Textbox control will render in your default web browser.

ASP.NET Core Numeric Textbox Control

Range validation

You can set the minimum and maximum range of values in the Numeric Textbox 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.

<ejs-numerictextbox id="numeric" step="2" value="16" min="10" max="20" Type="text"></ejs-numerictextbox>

Formatting the value

Users can set the format of the Numeric Textbox control using the Format property. The value will be displayed in the specified format when the control is in the focused-out state. For more information about formatting the value, refer to this link.

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

<ejs-numerictextbox id="numeric" value="10" format="c2" Type="text"></ejs-numerictextbox>

ASP.NET Core Numeric Textbox with Formatting

Precision of numbers

You can restrict the number of decimals to be entered in the Numeric Textbox by using the decimals and validateDecimalOnType properties.
So, you cannot enter a value with more decimal places than decimals allows.

  • If validateDecimalOnType is false, number of decimals will not be restricted.
    Otherwise, number of decimals will be restricted while typing in the Numeric Textbox.
<div id='container'>
    <div class='wrap'>
        <ejs-numerictextbox id="strict" value="10" validateDecimalOnType="true" decimals="3" format="n3" placeholder="ValidateDecimalOnType enabled" floatLabelType="Auto"></ejs-numerictextbox>
    </div>
    <div class='wrap'>
        <ejs-numerictextbox id="allow" value="10" decimals="3" format="n3" placeholder="ValidateDecimalOnType disabled" floatLabelType="Auto"></ejs-numerictextbox>
    </div>
</div>

Restricting Decimal Values in ASP.NET Core Numeric Textbox

NOTE

View Sample in GitHub

See also

  1. Getting Started with ASP.NET Core using Razor Pages
  2. Getting Started with ASP.NET Core MVC using Tag Helper
  3. How to perform custom validation using FormValidator
  4. How to customize the UI appearance of the control
  5. How to customize the spin button’s up and down arrow
  6. How to customize the step value and hide spin buttons
  7. How to prevent nullable input in Numeric Textbox
  8. How to maintain trailing zeros in Numeric Textbox