How to allow only numbers in textbox in reactjs? -


how allow numbers in textbox in reactjs using regular expression only?

for use regex: /[0-9]+/g;

in onchange method, check whether value proper of not, if value proper update state value otherwise ignore.

check example:

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'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>  <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>    <div id='app'/>


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -