I have an input field which I want to restrict to only input numbers. type="number"
works fine on computer and my android mobile but fails on iOS and I can still enter text in it. I looked around online and found this solution for the input field:
<input onkeypress="return event.charCode >= 48 && event.charCode <= 57" id="priceInput" type="text">
It works on all devices I have tried so far. However, I want to use this code for many input fields, and also do more things when pressing on a key in those input field. Therefore I want to run it as a JavaScript function in a separate file. I found this answer which looked promising and worked for android and desktop:
function isNumber(evt) {
evt = (evt) ? evt : window.event;
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
return true;
}
<input type="text" onkeypress="return isNumber(event)" />
However, this still fails on iOS and I can still put in all characters.
Questions
- How can I limit input field to only accept numbers on iOS phones?
- Do iOS have some blocker for functions to run on onkeypress?