0

I need to disable backspaces for all webpages in my project and i have done it using e.prevent default method, but when an alert is shown the e.prevent default is not working ,the webpage simply goes to the previous page when pressed backspace. Any solutions ?

if(input){
 alert("wrong input");
 }

function disableBackSpace(e){
   var doPrevent = false;
    if (e.keyCode === 8) {
        var d = e.srcElement || e.target;
        if ((d.tagName.toUpperCase() === 'INPUT' && 
             (
                 d.type.toUpperCase() === 'TEXT' ||
                 d.type.toUpperCase() === 'PASSWORD' || 
                 d.type.toUpperCase() === 'FILE' || 
                 d.type.toUpperCase() === 'SEARCH' || 
                 d.type.toUpperCase() === 'EMAIL' || 
                 d.type.toUpperCase() === 'NUMBER' || 
                 d.type.toUpperCase() === 'DATE' )
             ) || 
             d.tagName.toUpperCase() === 'TEXTAREA') {
            doPrevent = d.readOnly || d.disabled;
        }
        else {
            doPrevent = true;
        }
    }

    if (doPrevent) {
        e.preventDefault();
    }
}

The above method is called everytime i press a backspace key but this is not working when i'm on a alert window. Basically the above backspace code is included as a jsp page for all webpages.

George Kagan
  • 5,913
  • 8
  • 46
  • 50
vishalaestro
  • 125
  • 1
  • 1
  • 11
  • 1
    which **alert** you are talking about? show some code – Vikrant Nov 10 '16 at 10:21
  • Possible duplicate of [How can I disabling backspace key press on all browsers?](http://stackoverflow.com/questions/6309693/how-can-i-disabling-backspace-key-press-on-all-browsers) – Liam Nov 10 '16 at 10:29
  • non-jQuery version, probably more useful as a duplicate: [How to capture a backspace on the onkeydown event](http://stackoverflow.com/questions/2353550/how-to-capture-a-backspace-on-the-onkeydown-event) – Liam Nov 10 '16 at 10:37
  • The above code mentioned will only work when the focus is on the webpage u are viewing ,someone please display an alert window and also block the backspace when i press backspace when the alert is shown – vishalaestro Nov 10 '16 at 10:43

1 Answers1

0

Add your prevent Backspace code on $(document).keydown.

It will occur on the complete document, regardless of any alert or popup is open.

$(document).keydown(function(e) { 
            if (e.keyCode == 8) alert('Not Allowed');
    });

JsFiddle Demo

Vikrant
  • 4,920
  • 17
  • 48
  • 72
  • No mention of jQuery in the question or tags. **Note to the OP**: this won't work unless you include the jQuery libraries, which you may or may not want to do – Liam Nov 10 '16 at 10:31
  • same result not working , i have put this script right on the jsp page under the script tag – vishalaestro Nov 10 '16 at 10:36
  • even the code he mentioned above will not work ,remove the alert window and put e.preventDefault() .still the page will go back if we press backspace – vishalaestro Nov 10 '16 at 10:42