Answers for "allow only numbers in textbox react w3schools"

2

react allow only numbers in input

class App extends React.Component{
   constructor(){
      super();
      this.state = {value: ''};
      this.onChange = this.onChange.bind(this)
   }
   
   onChange(e){
      const re = /^[0-9\b]+$/;
      if (e.target.value === '' || re.test(e.target.value)) {
         this.setState({value: e.target.value})
      }
   }
   
   render(){
     return <input value={this.state.value} onChange={this.onChange}/>
   }
}

ReactDOM.render(<App/>,document.getElementById('app'))
Posted by: Guest on December-30-2020
0

How to make HTML input tag only accept numerical values? in jsx

<input
      onKeyPress={(event) => {
        if (!/[0-9]/.test(event.key)) {
          event.preventDefault();
        }
      }}
    />
Posted by: Guest on April-16-2021

Code answers related to "allow only numbers in textbox react w3schools"

Code answers related to "Javascript"

Browse Popular Code Answers by Language