close

[Solved] Only numbers. Input number in React

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Only numbers. Input number in React in javascript. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Only numbers. Input number in React Error Occurs?

Today I get the following error Only numbers. Input number in React in javascript.

How To Solve Only numbers. Input number in React Error ?

  1. How To Solve Only numbers. Input number in React Error ?

    To Solve Only numbers. Input number in React Error one line of code
    <input value={this.state.financialGoal} onChange={event => this.setState({financialGoal: event.target.value.replace(/\D/,'')})}/>

  2. Only numbers. Input number in React

    To Solve Only numbers. Input number in React Error one line of code
    <input value={this.state.financialGoal} onChange={event => this.setState({financialGoal: event.target.value.replace(/\D/,'')})}/>

Solution 1

one line of code

<input value={this.state.financialGoal} onChange={event => this.setState({financialGoal: event.target.value.replace(/\D/,'')})}/>

Solution 2

Simply way in React

<input
      onKeyPress={(event) => {
        if (!/[0-9]/.test(event.key)) {
          event.preventDefault();
        }
      }}
    />

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment