Similar Question
To restrict input to numbers only, use the input type number and set its minimum value to zero.
Next, add an event listener for key input on the number field and allow only numeric key presses.
Here's an example implementation:
// Get the input element.
var number = document.getElementById('number');
// Add an event listener for keydown on the number input.
number.onkeydown = function(e) {
if(!((e.keyCode > 95 && e.keyCode < 106)
|| (e.keyCode > 47 && e.keyCode < 58)
|| e.keyCode == 8)) {
return false;
}
}
<input type="number" id="number" min="0" />