HelpBot Assistant

How can I help you?

Getting Started with React NumericTextBox component

10 Feb 202616 minutes to read

This section explains the steps required to create a simple React NumericTextBox component and demonstrate its basic usage in a React environment.

Ready to streamline your Syncfusion® React development? Discover the full potential of Syncfusion® React components with Syncfusion® AI Coding Assistant. Effortlessly integrate, configure, and enhance your projects with intelligent, context-aware code suggestions, streamlined setups, and real-time insights—all seamlessly integrated into your preferred AI-powered IDEs like VS Code, Cursor, Syncfusion® CodeStudio and more. Explore Syncfusion® AI Coding Assistant.

To get started quickly with React NumericTextBox, you can watch this video:

Setup for local development

Easily set up a React application using create-vite-app, which provides a faster development environment, smaller bundle sizes, and optimized builds compared to traditional tools like create-react-app. For detailed steps, refer to the Vite installation instructions. Vite sets up your environment using JavaScript and optimizes your application for production.

Note: To create a React application using create-react-app, refer to this documentation for more details.

To create a new React application, run the following command.

npm create vite@latest my-app

This command will prompt you for a few settings for the new project, such as selecting a framework and a variant.

Initial_setup

To set up a React application in TypeScript environment, run the following command.

npm create vite@latest my-app -- --template react-ts
cd my-app
npm run dev

To set up a React application in JavaScript environment, run the following command.

npm create vite@latest my-app -- --template react
cd my-app
npm run dev

Adding Syncfusion® NumericTextBox packages

All the available Essential® JS 2 packages are published in the npmjs.com public registry.
To install the NumericTextBox component, use the following command

npm install @syncfusion/ej2-react-inputs --save

The –save will instruct NPM to include the NumericTextBox package inside of the dependencies section of the package.json.

Adding CSS reference

The following CSS files are available in the ../node_modules/@syncfusion package folder. Add these as references in src/App.css.

@import "../node_modules/@syncfusion/ej2-base/styles/tailwind3.css";
@import "../node_modules/@syncfusion/ej2-react-inputs/styles/tailwind3.css";

To refer App.css in the application then import it in the src/App.tsx file.

Adding NumericTextBox component

The React NumericTextBox component can be added to the application by following these steps. To get started, add the NumericTextBox component to the src/App.tsx file using the following code.

The following numerictextbox code should be placed in the src/App.tsx file.

[Class-component]

import * as React from "react";
import { NumericTextBoxComponent } from '@syncfusion/ej2-react-inputs';
import './App.css';

export default class App extends React.Component<{}, {}> {
  render() {
    return (
      <NumericTextBoxComponent value={10} />
    );
  }
}

[Functional-component]

import * as React from 'react';
import { NumericTextBoxComponent } from '@syncfusion/ej2-react-inputs';
import './App.css';

export default function App(){
  return (
    <NumericTextBoxComponent value={10} />
  );
}

Run the application

Run the npm run dev command in the terminal to start the development server. This command compiles your code and serves the application locally, opening it in the browser.

npm run dev

The output appears as follows.

[Class-component]

import { NumericTextBoxComponent } from '@syncfusion/ej2-react-inputs';
import * as React from "react";
import * as ReactDOM from "react-dom";

// initializes NumericTextBox component
ReactDOM.render(<NumericTextBoxComponent value={10} />, document.getElementById('numericContainer'));
import { NumericTextBoxComponent } from '@syncfusion/ej2-react-inputs';
import * as React from "react";
import * as ReactDOM from "react-dom";

// initializes NumericTextBox component
ReactDOM.render(<NumericTextBoxComponent value={10} />, document.getElementById('numericContainer'));

[Functional-component]

import { NumericTextBoxComponent } from '@syncfusion/ej2-react-inputs';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

// initializes NumericTextBox component
function App() {
    return <NumericTextBoxComponent value={10} />;
}

ReactDOM.render(<App />, document.getElementById('numericContainer'));
import { NumericTextBoxComponent } from '@syncfusion/ej2-react-inputs';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

// initializes NumericTextBox component
function App() {
  return <NumericTextBoxComponent value={10} />;
}

ReactDOM.render(<App />, document.getElementById('numericContainer'));

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.

[Class-component]

import { NumericTextBoxComponent } from '@syncfusion/ej2-react-inputs';
import * as React from "react";
import * as ReactDOM from "react-dom";

// sets the minimum and maximum range values
ReactDOM.render(<NumericTextBoxComponent min={10} max={20} value={16} step={2} />, document.getElementById('numericContainer'));
import { NumericTextBoxComponent } from '@syncfusion/ej2-react-inputs';
import * as React from "react";
import * as ReactDOM from "react-dom";

// sets the minimum and maximum range values
ReactDOM.render(<NumericTextBoxComponent min={10} max={20} value={16} step={2} />, document.getElementById('numericContainer'));

[Functional-component]

import { NumericTextBoxComponent } from '@syncfusion/ej2-react-inputs';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

// sets the minimum and maximum range values
function App() {
    return <NumericTextBoxComponent min={10} max={20} value={16} step={2} />;
}

ReactDOM.render(<App />, document.getElementById('numericContainer'));
import { NumericTextBoxComponent } from '@syncfusion/ej2-react-inputs';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

// sets the minimum and maximum range values
function App() {
  return <NumericTextBoxComponent min={10} max={20} value={16} step={2} />;
}

ReactDOM.render(<App />, document.getElementById('numericContainer'));

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.

[Class-component]

import { NumericTextBoxComponent } from '@syncfusion/ej2-react-inputs';
import * as React from "react";
import * as ReactDOM from "react-dom";

// sets currency with 2 numbers of decimal places format
ReactDOM.render(<NumericTextBoxComponent format='c2' value={10} />, document.getElementById('numericContainer'));
import { NumericTextBoxComponent } from '@syncfusion/ej2-react-inputs';
import * as React from "react";
import * as ReactDOM from "react-dom";

// sets currency with 2 numbers of decimal places format
ReactDOM.render(<NumericTextBoxComponent format='c2' value={10} />, document.getElementById('numericContainer'));

[Functional-component]

import { NumericTextBoxComponent } from '@syncfusion/ej2-react-inputs';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

// sets currency with 2 numbers of decimal places format
function App() {
    return <NumericTextBoxComponent format="c2" value={10} />;
}

ReactDOM.render(<App />, document.getElementById('numericContainer'));
import { NumericTextBoxComponent } from '@syncfusion/ej2-react-inputs';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

// sets currency with 2 numbers of decimal places format
function App() {
  return <NumericTextBoxComponent format="c2" value={10} />;
}

ReactDOM.render(<App />, document.getElementById('numericContainer'));

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.

[Class-component]

import { NumericTextBoxComponent } from '@syncfusion/ej2-react-inputs';
import * as React from "react";
import * as ReactDOM from "react-dom";

// restricts number of decimals to be entered in the NumericTextBox by enabling validateDecimalOnType property
// sets number of decimal places to be allowed by the NumericTextBox
// sets number with 3 numbers of decimal places format
ReactDOM.render(<NumericTextBoxComponent validateDecimalOnType={true} decimals={3} format='n3' value={10} placeholder='ValidateDecimalOnType Enabled' floatLabelType='Auto' />, document.getElementById('numericContainer1'));

// validateDecimalOnType is false by default. So, number of decimals will not be restricted.
// sets number of decimal places to be allowed by the NumericTextBox
// sets number with 3 numbers of decimal places format
ReactDOM.render(<NumericTextBoxComponent decimals={3} format='n3' value={10} placeholder='ValidateDecimalOnType Disabled' floatLabelType='Auto' />, document.getElementById('numericContainer2'));
import { NumericTextBoxComponent } from '@syncfusion/ej2-react-inputs';
import * as React from "react";
import * as ReactDOM from "react-dom";

// restricts number of decimals to be entered in the NumericTextBox by enabling validateDecimalOnType property
// sets number of decimal places to be allowed by the NumericTextBox
// sets number with 3 numbers of decimal places format
ReactDOM.render(<NumericTextBoxComponent validateDecimalOnType={true} decimals={3} format='n3' value={10} placeholder='ValidateDecimalOnType Enabled' floatLabelType='Auto' />, document.getElementById('numericContainer1'));

// validateDecimalOnType is false by default. So, number of decimals will not be restricted.
// sets number of decimal places to be allowed by the NumericTextBox
// sets number with 3 numbers of decimal places format
ReactDOM.render(<NumericTextBoxComponent decimals={3} format='n3' value={10} placeholder='ValidateDecimalOnType Disabled' floatLabelType='Auto' />, document.getElementById('numericContainer2'));

[Functional-component]

import { NumericTextBoxComponent } from '@syncfusion/ej2-react-inputs';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

// restricts number of decimals to be entered in the NumericTextBox by enabling validateDecimalOnType property
// sets number of decimal places to be allowed by the NumericTextBox
// sets number with 3 numbers of decimal places format
function App1() {
    return (<NumericTextBoxComponent validateDecimalOnType={true} decimals={3} format="n3" value={10} placeholder="ValidateDecimalOnType Enabled" floatLabelType="Auto" />);
}
ReactDOM.render(<App1 />, document.getElementById('numericContainer1'));

// validateDecimalOnType is false by default. So, number of decimals will not be restricted.
// sets number of decimal places to be allowed by the NumericTextBox
// sets number with 3 numbers of decimal places format
function App2() {
    return (<NumericTextBoxComponent decimals={3} format="n3" value={10} placeholder="ValidateDecimalOnType Disabled" floatLabelType="Auto" />);
}
ReactDOM.render(<App2 />, document.getElementById('numericContainer2'));
import { NumericTextBoxComponent } from '@syncfusion/ej2-react-inputs';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

// restricts number of decimals to be entered in the NumericTextBox by enabling validateDecimalOnType property
// sets number of decimal places to be allowed by the NumericTextBox
// sets number with 3 numbers of decimal places format
function App1() {
  return (
    <NumericTextBoxComponent
      validateDecimalOnType={true}
      decimals={3}
      format="n3"
      value={10}
      placeholder="ValidateDecimalOnType Enabled"
      floatLabelType="Auto"
    />
  );
}
ReactDOM.render(<App1 />, document.getElementById('numericContainer1'));

// validateDecimalOnType is false by default. So, number of decimals will not be restricted.
// sets number of decimal places to be allowed by the NumericTextBox
// sets number with 3 numbers of decimal places format
function App2() {
  return (
    <NumericTextBoxComponent
      decimals={3}
      format="n3"
      value={10}
      placeholder="ValidateDecimalOnType Disabled"
      floatLabelType="Auto"
    />
  );
}
ReactDOM.render(<App2 />, document.getElementById('numericContainer2'));

Refer to the React NumericTextBox feature tour page for its groundbreaking feature representations. You can also explore our React NumericTextBox component example that shows how to render the NumericTextBox in React.

See Also