How can I help you?
Getting started in EJ2 JavaScript NumericTextBox control
10 Feb 202611 minutes to read
This section explains the steps required to create a simple Essential® JS 2 Numeric textbox and demonstrate the basic usage of the Numeric textbox control in a JavaScript application.
Dependencies
The list of dependencies required to use the Numeric textbox component in your application is given below:
|-- @syncfusion/ej2-inputs
|-- @syncfusion/ej2-baseSetup for local environment
Refer to the following steps to set up your local environment.
Step 1: Create a root folder named my-app for your application.
Step 2: Create a my-app/resources folder to store local scripts and styles files.
Step 3: Open Visual Studio Code and create my-app/index.js and my-app/index.html files to initialize the Essential® JS 2 Numeric textbox control.
Add NumericTextBox to the project
Add an HTML input element for the NumericTextBox in your index.html file.
[src/index.html]
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 NumericTextBox component</title>
<!-- Essential JS 2 Input's's dependent material theme -->
<link href="//cdn.syncfusion.com/ej2/ej2-base/styles/material.css" rel="stylesheet" type="text/css"/>
<link href="//cdn.syncfusion.com/ej2/ej2-inputs/styles/material.css" rel="stylesheet" type="text/css"/>
<!-- Essential JS 2 all script -->
<!-- <script src="//cdn.syncfusion.com/ej2/dist/ej2.min.js" type="text/javascript"></script> -->
<!-- Essential JS 2 Input's's dependent scripts -->
<script src="//cdn.syncfusion.com/ej2/ej2-base/dist/global/ej2-base.min.js" type="text/javascript"></script>
<script src="//cdn.syncfusion.com/ej2/ej2-inputs/dist/global/ej2-inputs.min.js" type="text/javascript"></script>
</head>
<body>
<div>
<!--element which is going to render the NumericTextBox -->
<input id="numeric" type="text" />
</div>
<script>
// initializes NumericTextBox component
var numeric = new ej.inputs.NumericTextBox({
// sets value to the NumericTextBox
value: 10
});
// renders initialized NumericTextBox
numeric.appendTo('#numeric');
</script>
</body>
</html>Run the application
Now use the npm run start command to run the application in the browser.
npm run start
The following example demonstrates the NumericTextBox.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 NumericTextBox component</title>
<!-- Essential JS 2 Input's's dependent material theme -->
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-base/styles/material.css" rel="stylesheet" type="text/css"/>
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-inputs/styles/material.css" rel="stylesheet" type="text/css"/>
<!-- Essential JS 2 all script -->
<!-- <script src="//cdn.syncfusion.com/ej2/21.2.3/dist/ej2.min.js" type="text/javascript"></script> -->
<!-- Essential JS 2 Input's's dependent scripts -->
<script src="//cdn.syncfusion.com/ej2/21.2.3/ej2-base/dist/global/ej2-base.min.js" type="text/javascript"></script>
<script src="//cdn.syncfusion.com/ej2/21.2.3/ej2-inputs/dist/global/ej2-inputs.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='container' style="width: 240px; margin: 0 auto;padding-top:100px;">
<!--element which is going to render the NumericTextBox -->
<input id="numeric" type="text" />
</div>
<script>
// initializes NumericTextBox component
var numeric = new ej.inputs.NumericTextBox({
// sets value to the NumericTextBox
value: 10
});
// renders initialized NumericTextBox
numeric.appendTo('#numeric');
</script>
</body>
</html>Range validation
You can set the minimum and maximum range of values in the NumericTextBox using the min and max properties. The numeric value must fall within the specified range.
The validation behavior depends on the strictMode property.
The following example demonstrates range validation.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 NumericTextBox component</title>
<!-- Essential JS 2 Input's's dependent material theme -->
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-base/styles/material.css" rel="stylesheet" type="text/css"/>
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-inputs/styles/material.css" rel="stylesheet" type="text/css"/>
<!-- Essential JS 2 all script -->
<!-- <script src="//cdn.syncfusion.com/ej2/21.2.3/dist/ej2.min.js" type="text/javascript"></script> -->
<!-- Essential JS 2 Input's's dependent scripts -->
<script src="//cdn.syncfusion.com/ej2/21.2.3/ej2-base/dist/global/ej2-base.min.js" type="text/javascript"></script>
<script src="//cdn.syncfusion.com/ej2/21.2.3/ej2-inputs/dist/global/ej2-inputs.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='container' style="width: 240px; margin: 0 auto;padding-top:100px;">
<!--element which is going to render the NumericTextBox -->
<input id="numeric" type="text" />
</div>
<script>
// initializes NumericTextBox component
var numeric = new ej.inputs.NumericTextBox({
// sets the minimum range value
min: 10,
// sets the maximum range value
max: 20,
// sets value to the NumericTextBox
value: 16,
// sets the step value to increment or decrement value of the NumericTextBox
// based on the step value.
step:2
});
// renders initialized NumericTextBox
numeric.appendTo('#numeric');
</script>
</body>
</html>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 following example demonstrates formatting the value using currency format c2.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 NumericTextBox component</title>
<!-- Essential JS 2 Input's's dependent material theme -->
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-base/styles/material.css" rel="stylesheet" type="text/css"/>
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-inputs/styles/material.css" rel="stylesheet" type="text/css"/>
<!-- Essential JS 2 all script -->
<!-- <script src="//cdn.syncfusion.com/ej2/21.2.3/dist/ej2.min.js" type="text/javascript"></script> -->
<!-- Essential JS 2 Input's's dependent scripts -->
<script src="//cdn.syncfusion.com/ej2/21.2.3/ej2-base/dist/global/ej2-base.min.js" type="text/javascript"></script>
<script src="//cdn.syncfusion.com/ej2/21.2.3/ej2-inputs/dist/global/ej2-inputs.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='container' style="width: 240px; margin: 0 auto;padding-top:100px;">
<!--element which is going to render the NumericTextBox -->
<input id="numeric" type="text" />
</div>
<script>
// initializes NumericTextBox component
var currency = new ej.inputs.NumericTextBox({
// sets currency with 2 numbers of decimal places format
format: 'c2',
// sets value to the NumericTextBox
value: 10
});
// renders initialized NumericTextBox
currency.appendTo('#numeric');
</script>
</body>
</html>Precision of numbers
You can restrict the number of decimals to be entered in the NumericTextBox using the decimals and validateDecimalOnType properties. You cannot enter numbers with precision greater than the specified decimal places.
- If
validateDecimalOnTypeis false, number of decimals will not be restricted. Else, number of decimals will be restricted while typing in the NumericTextBox.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 NumericTextBox component</title>
<!-- Essential JS 2 Input's's dependent material theme -->
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-base/styles/material.css" rel="stylesheet" type="text/css"/>
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-inputs/styles/material.css" rel="stylesheet" type="text/css"/>
<!-- Essential JS 2 all script -->
<!-- <script src="//cdn.syncfusion.com/ej2/21.2.3/dist/ej2.min.js" type="text/javascript"></script> -->
<!-- Essential JS 2 Input's's dependent scripts -->
<script src="//cdn.syncfusion.com/ej2/21.2.3/ej2-base/dist/global/ej2-base.min.js" type="text/javascript"></script>
<script src="//cdn.syncfusion.com/ej2/21.2.3/ej2-inputs/dist/global/ej2-inputs.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='container'>
<div style="width: 240px; margin: 0 auto;padding-top:100px;">
<input id="strict" type="text" />
</div>
<div style="width: 240px; margin: 0 auto;padding-top:20px;">
<input id="allow" type="text" />
</div>
</div>
<script>
// initializes NumericTextBox component
var numeric = new ej.inputs.NumericTextBox({
// restricts number of decimals to be entered in the NumericTextBox
validateDecimalOnType: true,
// sets number of decimal places to be allowed by the NumericTextBox
decimals: 3,
// sets number with 3 numbers of decimal places format
format: 'n3',
value: 10,
placeholder: 'ValidateDecimalOnType enabled',
floatLabelType: 'Auto'
});
// renders initialized NumericTextBox
numeric.appendTo('#strict');
var numeric1 = new ej.inputs.NumericTextBox({
// sets number of decimal places to be allowed by the NumericTextBox
decimals: 3,
// sets number with 3 numbers of decimal places format
format: 'n3',
value: 10,
placeholder: 'ValidateDecimalOnType disabled',
floatLabelType: 'Auto'
});
// renders initialized NumericTextBox
numeric1.appendTo('#allow');
</script>
</body>
</html>See Also
- How to perform custom validation using FormValidator
- How to customize the UI appearance of the control
- How to customize the spin button’s up and down arrow
- How to customize the step value and hide spin buttons
- How to prevent nullable input in NumericTextBox
- How to maintain trailing zeros in NumericTextBox