Disable Backspace key in browser except in text boxes

I found the following code here.


// Trap Esc(27), Backspace(8) and Enter(13) -
// Except bksp on text/textareas/password, enter on textarea/submit/link
if (typeof window.event != 'undefined') {
  document.onkeydown = function() {
    var t = event.srcElement.type;
    var kc = event.keyCode;
    //alert('Type: ' + t);
    // Type = '' is what I get from the A HREF elements that need to remain functional in my form
    return ((kc != 8 && kc != 13 && kc != 27) || (t == 'text' && kc != 13 && kc != 27) ||
            (t == 'textarea' && kc != 27) || (t == 'button' && kc == 13) || (t == 'submit' && kc == 13) ||
            (t == 'password' && kc != 27 && kc != 13) || (t == '' && kc == 13));
    }
} else {
  document.onkeypress = function(e) {
    // FireFox/Others
    var t = e.target.type;
    var kc = e.keyCode;
    if ((kc != 8 && kc != 13 && kc != 27) || (t == 'text' && kc != 13 && kc != 27) ||
       (t == 'textarea' && kc != 27) || (t == 'button' && kc == 13) || (t == 'submit' && kc == 13) ||
       (t == 'password' && kc != 27 && kc != 13) || (t == '' && kc == 13)) {
        return true;
    } else {
        return false;
    }
  }
}
Leave a comment

0 Comments.

Leave a Reply


[ Ctrl + Enter ]