我使用keycode对输入文本框应用了某些验证。我如何使用unicode做到这一点。在下面的代码中,我限制了文本框中的特殊字符,如使用keycode所见,但我想使用unicode进行限制。
$('.alphaOnly').keydown(function (e) {
var pressedkey = e.keyCode;
if ($(this).getCursorPosition() == 1 || $(this).getCursorPosition() == 0) {
if (pressedkey == 32) {
e.preventDefault();
}
}
if (!((pressedkey == 8) || (pressedkey == 9) || (pressedkey == 32) || (pressedkey == 46) || (pressedkey >= 35 && pressedkey <= 40) || (pressedkey == 17) || (pressedkey == 18) || (pressedkey == 19) || (pressedkey >= 65 && pressedkey <= 90))) {
e.preventDefault();
}
});
使用e.charCode
(现在被e.char
淘汰)
参考:https://developer.mozilla.org/en-US/docs/Web/Events/keydown