Yes, the shift and hold shift key affects keyCode. It is better to use the which property, since in firefox, keyCode returns zero.
Here is an example.
var inp = document.getElementsByTagName("input")[0]; inp.addEventListener("keypress", function(event) { if (event.which < 48 || event.which > 57) { event.preventDefault(); } });
<input type="text" placeholder="Только цифры">
Also as an option you can use the mask - Masked Input Plugin
Example
jQuery(function($){ $("#digits").mask("999999999",{placeholder:" "}); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.maskedinput/1.4.1/jquery.maskedinput.min.js"></script> <input type="text" id="digits" placeholder="Только цифры">
But you need to specify the number of characters to enter.
Try these options. Should work. It is difficult to say that you have, without examples.
<input type="number" />not tried? - Darthe.which? - Surfin Bird