Created
June 30, 2016 11:22
-
-
Save kvalexandr/44dbf7fba6903b7560d15a84034b9221 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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