Skip to content

Instantly share code, notes, and snippets.

@kvalexandr
Created June 30, 2016 11:22
Show Gist options
  • Save kvalexandr/44dbf7fba6903b7560d15a84034b9221 to your computer and use it in GitHub Desktop.
Save kvalexandr/44dbf7fba6903b7560d15a84034b9221 to your computer and use it in GitHub Desktop.
function getChar(event) {
if (event.which == null) { // IE
if (event.keyCode < 32) return null; // спец. символ
return String.fromCharCode(event.keyCode)
}
if (event.which != 0 && event.charCode != 0) { // все кроме IE
if (event.which < 32) return null; // спец. символ
return String.fromCharCode(event.which); // остальные
}
return null; // спец. символ
}
$(document).on("keypress", ".product-cnt", function (e) {
e = e || event;
if (e.ctrlKey || e.altKey || e.metaKey) return;
var chr = getChar(e);
// с null надо осторожно в неравенствах,
// т.к. например null >= '0' => true
// на всякий случай лучше вынести проверку chr == null отдельно
if (chr == null) return;
if (chr < '0' || chr > '9') {
return false;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment