I have written a JS function that only allow numbers to be entered. A copy of that function is below:
function NumbersOnly(e) {
var evt = e || window.eve
use keypress for holding any key isntead of keydown
Use event.key
instead of charCode
. No more magic numbers!
function onEvent(event) {
const key = event.key; // "a", "1", "Shift", etc.
if (isFinite(key)) { // Is number
// Do work
}
};
Mozilla Docs
Supported Browsers
You can check if shift key is pressed using :
if(evt.shiftKey) {
... //returns true if shift key is pressed