I'm making an app in which my inputs are controlled. I want a particular input to accept numbers only and I have achieved it, but it won't take numpad inputs for it takes them as letters (code range is 96 to 105)
This is the input:
<input onKeyDown = {(e) => this.handleChange(e)} type="text" value = {this.state.inputValue} />
And my function:
handleChange(e){
let value = this.state.inputValue;
if(e.keyCode >= 48 && e.keyCode <= 57 && value.length < 4)
{
this.setState(
{
inputValue: value + String.fromCharCode(e.keyCode)
}
);
}
else if(e.keyCode == 8)
{
this.setState(
{
inputValue: value.substring(0, value.length - 1)
}
);
}
}