I’m designing a web app that features an editable HTML table. I want to allow the user to input a single value in the range 1-9 only. I want the user to be able to change the input as needed or remove it altogether with backspace. I’m trying to handle this with JavaScript using the onkeydown argument to the td tags. Here is the HTML with JavaScript that I’m using. On desktop this works great, and only allows a single value input, replaces the value when a new one is pressed, and allows the value to be removed using the backspace (keyCode === 8). However, on Android something very weird happens – the numeric keypad that comes up with inputmode=numeric contains a few special characters, namely ,, ., -, ␣. For some reason these do not get picked up and handled in the same way as other inputs. Trying to debug this, I’ve found that the keyCode for all of these inputs is 229, and the key attribute is undefined. I’ve tried adding a handler for keyCode === 229 but this doesn’t stop these keys changing the input. I’ve also added a screenshot of the input when these special keys are pressed.
<head>
<meta charset="UTF-8">
</head>
<body>
</script>
<style>
table { border-collapse: collapse; font-family: Calibri, sans-serif; }
td { border: solid thin; height: 2.3em; width: 2.3em; text-align: center; padding: 0; }
</style>
<table id="table" class="centre">
<tbody>
<tr> <td contentEditable inputmode=numeric onkeydown="return testCharacter(event);"></td>
</table>
<script>
function testCharacter(evt) {
var charCode = (evt.which) ? evt.which : evt.keyCode, self = evt.target;
if ((evt.keyCode >= 49 && evt.keyCode <= 57)) {
self.innerText = charCode - 48;
return false;
} else if (evt.keyCode === 8) {
self.innerText = null;
return false;
} else {
return false;
};
};
</script>
</body>


