The simplest way will be to convert the string to a number and then check if it's valid. As the value of inputs is always a string even if you use type="number", though it is good to use it if you just want numbers as the input.

You can use isNaN(+value). Here + will convert a string to a number.

<input
  type="text"
  onChange={(e) => {
    const value = e.target.value;
    console.log(!isNaN(+value)); // true if its a number, false if not
  }}
/>


Some test cases:

console.log(!isNaN(+"54"));
console.log(!isNaN(+"23xede"));
console.log(!isNaN(+"test"));
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Note: As mentioned this is a simple way, there can be workarounds to bypass this, for a robust solution you can have a look here: Check whether variable is number or string in JavaScript

Answer from Manas Khandelwal on Stack Overflow
🌐
About React
aboutreact.com › home › check value is a number using isnan() in react native
Check Value is a Number using isNaN() in React Native - About React
November 21, 2020 - The function isNaN() is used to check the value of any variable or state is a number or not. In this example we will check input is a number or not. isNaN(Pass the Value to check); //It will return true if not a number else false
Discussions

React Input validation if empty or number
One solution assuming you want a one size fits all error message only checking if it was a number or not would be to put them into an array and set error if the input is not a number. More on stackoverflow.com
🌐 stackoverflow.com
reactjs - How do I validate a number input within a React component - Stack Overflow
In my return statement, I try to check for a valid number or else assign the value 0. This doesn't seem to work, is there a different way of doing this? return ( More on stackoverflow.com
🌐 stackoverflow.com
Only numbers. Input number in React - javascript
No matter how the user attempts to populate this text input (typing and pasting being the most common ways), only numbers will be allowed. 2018-01-31T16:13:21.14Z+00:00 ... Yup. This one right here. I tried the accepted answer, but e.target.validity.valid kept returning false, even when matching the pattern. 2019-07-02T05:38:31.96Z+00:00 ... Only issue is this take cursor to the end when I try to add character in the middle 2023-10-14T04:10:59.6Z+00:00 ... If ... More on stackoverflow.com
🌐 stackoverflow.com
How to only accept numbers in a input[type=text]
use type=number alternatively: More on reddit.com
🌐 r/react
22
22
May 12, 2024
🌐
Bobby Hadz
bobbyhadz.com › blog › react-only-number-input
Create a Numbers only Input field in React.js | bobbyhadz
We set the onChange prop on the input field, so every time its value changes, the handleChange function is invoked. ... In our handleChange() function, we update the num state variable and check if the value is a valid number.
🌐
Font Awesome
fontawesomeicons.com › fa › react-js-check-value-is-integer-or-string
React Js Check Value is Integer or String | isInteger(myNumber), !isNaN(inputValue)
If inputValue is a number, it displays "It is a number," and if it's not a number, it displays "It is not a number." The result is shown below the input field and updates dynamically as the user types. This code demonstrates how React can be used to check and display whether the input value is a number or a string in a user-friendly manner
Top answer
1 of 4
8

If you want to keep your error messages separate I would recommend to reorganize your state.

So scalable solution (you may add more controls by just adding them to state) may look like:

class NumberControlsWithErrorMessage extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      inputs: [
        { name: 'price', value: 0, error: ''},
        { name: 'downP', value: 0, error: '' },
        { name: 'term', value: 0, error: '' },
        { name: 'interest', value: 0, error: '' }
      ]
    };
  }

  handleInputChange = (idx, event) => {
    const target = event.target;
    const name = target.name;
    let error = '';

    if (isNaN(target.value)) {
      error = `${name} field can only be number`
    }

    if (!target.value) {
      error = `${name} field cannot be empty`
    }

    this.state.inputs[idx] = {
      ...this.state.inputs[idx],
       value: target.value,
      error
    }

    this.setState({
      inputs: [...this.state.inputs]
    });
  }

  render() {
    return (
      <form>
        {this.state.inputs.map((input, idx) => (
          <div>
          <label htmlFor="">{input.name}</label>
          <input type="text" value={input.value} onChange={(e) => this.handleInputChange(idx, e)}/>
          {input.error && <span>{input.error}</span> }
          </div>
        ))}
      </form>
    );
  }
}

Working example

Also if you are building a complex form, you may want to try some React solution for forms, where all the mechanism for listening to events, state updates, validatoin are already handled for you. Like reactive-mobx-form

2 of 4
4

A straightforward way of handling multiple objects needing validation is to store an errors object in your state that has a property for each input field. Then you conditionally render the error underneath each input field depending on whether or not it has an error. Here is a very basic example:

class Calculator extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      price: 0, downP: 0, term: 0, interest: 0,
      errors: { price: '', downP: '', term: '', interest: '' }
    };
  }

  handleValidation = () => {
    const { price, downP, loan, interest } = this.state;
    let errors = { price: '', downP: '', term: '', interest: '' };

    if (!price) {
     errors.price = 'Price is required';
    } else if (isNaN(price)) {
      errors.price = 'Price must be a number';
    }

    if (!downP) {
      errors.downP = 'Down Payment is required';
    }

    // Rest of validation conditions go here...

    this.setState({ errors });
  }

  render() {
    const { errors } = this.state;

    return (
      <form>
        <input name="price" value={this.state.price} onChange={this.handleChange} />
        {errors.price != '' && <span style={{color: "red"}}>{this.state.errors.price}</span>}
        <input name="downP" value={this.state.downP} onChange={this.handleChange} />
        {errors.downP != '' && <span style={{color: "red"}}>{this.state.errors.downP}</span>}

        {/** Rest of components go here */}
      </form>
    );
  }
}

You can choose whether or not to run validation once the form submits or on every keypress and that will affect how when those messages appear and disappear, but this should give you an idea on how you would manage error messages specific to each input field.

🌐
AbstractAPI
abstractapi.com › api guides, tips & tricks › how to validate and verify your phone in react and react native
How to Validate a Phone Number in React and React Native | Abstract
July 30, 2026 - React Native phone verification ... first, your backend sends an OTP to the user's phone number via an SMS service; second, the user enters the code in the app and your backend verifies it through the corresponding API endpoint...
🌐
Atomic Spin
spin.atomicobject.com › 2019 › 04 › 24 › react-integer-input
Intuitive React Integer Input & Integer Validation
September 19, 2022 - If a min or max is set, input outside their range is considered invalid. ... That are less than the min will change to the min when focus is lost. That are greater than the max will change to the max when focus is lost. The expanded rules handling invalid input are not only intuitive to the user, but they also make the boundaries discoverable. ... // IntegerInput.jsx import React, {useState} from 'react'; const isValid = (value, min, max) => value !== '' && value !== '-' && (min === undefined || value >= min) && (max === undefined || value <= max); export const IntegerInput = ({ value, min, max, onChange }) => { const regexp = new RegExp(`^-?[0-9]*$`); const [internalValue, setInternalValue] = useState(value); const [valid, setValid] = useState(isValid(value, min, max)); return ( <input type="text" className={ valid ?
Find elsewhere
🌐
React
react.dev › reference › react-dom › components › input
<input> – React
If true, the input is not editable by the user. required: A boolean. If true, the value must be provided for the form to submit. size: A number. Similar to setting width, but the unit depends on the control. src: A string. Specifies the image source for a type="image" input. step: A positive number or an 'any' string. Specifies the distance between valid values. ... Checkboxes need checked (or defaultChecked), not value (or defaultValue).
🌐
GeeksforGeeks
geeksforgeeks.org › reactjs › how-to-validate-mobile-number-length-in-reactjs
How to validate mobile number length in ReactJS ? - GeeksforGeeks
July 23, 2025 - Example: This example implements a form with mobile number input field validated using regex. ... import React, { useState } from 'react'; const App = () => { const [mobileNumber, setMobileNumber] = useState(''); const [message, setMessage] = useState(''); const handleInputChange = (event) => { const value = event.target.value; if (/^\d*$/.test(value)) { setMobileNumber(value); // Regular expression to match exactly 10 digits const regex = /^\d{10}$/; if (value.length === 0 || regex.test(value)) { setMessage(value.length === 10 ?
🌐
DEV Community
dev.to › anilsingh › allow-only-numbers-in-input-in-react-2m71
Allow only numbers in Input in React - DEV Community
December 3, 2019 - Use value and onChange property of input field to allow only numbers in textbox. Inside the onChange handle check whether the entered value is valid number or not. Update the state only when entered value is a valid number.
🌐
Plain English
plainenglish.io › home › blog › javascript › how to check if a variable is a number in javascript
How To Check If A Variable Is A Number In JavaScript
January 28, 2021 - I was building a form the other day in Vue and I had to write number validation for the field so had to write the logic to check whether the input value is number or not.
🌐
Reddit
reddit.com › r/reactjs › how to prevent entering non-digits inside a custom input component?
r/reactjs on Reddit: How to prevent entering non-digits inside a custom input component?
April 10, 2024 -

The UI Kit we use in our company has some input component that allows to enter any symbol. But I need to make it a numeric field. So I need to ignore any non-digit symbols that a user tries to enter in this field. For that on every key down I check the key with isFinite() and if it's not then I just prevent the default behavior.
But sometimes a user needs to use combinations like ctrl+A or some other ones. So my solution is not good because I also need to check for all possible shortcuts.

Also worth to mention that the input is controlled by React Form Hook and not useState