javascript - Regex disabling escape key -
i using following code try , allow letters, numbers, backspace, space, dashes, , ampersands. want "disabled" other keys including function keys (f1, f2, etc). problem running in regex disabling escape key.
can same thing achieved without using regular expression?
this function part of live search feature, , don't want ajax request sent if on of "prohibited" keys pressed.
searchbox.keyup(function (e) { // live search function var functionkeyspressed = [112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123].indexof(e.which) > -1; if (string.fromcharcode(e.which).match(/^[\w\x08]$/) && !functionkeyspressed) { // code execute if key allowed } });
you can without regex. simple refer e.keycode
instead of e.which
:
if (e.keycode == 27){ //esc key pressed }
for onkeyup
keycode returns unicode keycode of key triggered event.
you find keycodes here: http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes
to exclude multiple keys, create array , check if keycode blacklisted:
var blacklisted = [1,2,3,4,5]; if (blacklisted.indexof(e.keycode) == -1){ //do }
Comments
Post a Comment