I'm working on my ReactJS new website and I want the input to allow the user to type only digits for mobile number.
onlyNumberKey = (event) => {
let ASCIICode = event.which ? event.which : event.keyCode;
if (ASCIICode > 31 && (ASCIICode < 48 || ASCIICode > 57))
return false;
return true;
};
<div>
<input type='text' onKeyPress='return {this.onlyNumberKey}' />
</div>
I use the "onlyNumberKey" function I found in many sites I discovered for my question.
This fuction is working and will well return true or false as needed,
but
I probably don't understad, how to prevent letters and special chars from being insert by the user ?
This wont work and gives an error -
onKeyPress='return this.onlyNumberKey'
"Warning: Expected onKeyPress listener to be a function, instead got a value of string type."
And I know why, just to clear that I tried many solutions.