我需要限制特殊字符(如“/”、“\”)以及“$”(shift+4)、“#”(shift+3)的输入,但还需要允许输入大写字符,如“A” (shift+a) 等。
下面的代码有效,但我不限制“$”和所有(从“shift+0”到“shift+9”),因为我允许“shift”,给我一些回应如何做到这一点,
HTML
<input type="text" id="txtboxToFilter" />
jQuery
<script type="text/javascript" language="javascript">
$(document).ready(function () {
$("#txtboxToFilter").keydown(function (event) {
// Allow: backspace, delete, tab, escape, and enter
if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 ||
// Allow: Ctrl+A
(event.keyCode == 65 && event.ctrlKey === true) ||
// Allow: home, end, left, right
(event.keyCode >= 35 && event.keyCode <= 39)) {
// let it happen, don't do anything
return;
}
else {
// stop the keypress for special character "/", "\"
if (event.keyCode >= 190) {
event.preventDefault();
}
}
});
});