Click here to Skip to main content
15,071,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to avoid backspace fuctionality in my Login page.
when I press Backspace the page would be reload.
Posted

C#
//Try this. It will prevent backspace except in the case of textareas and text inputs 
    $(document).keydown(function (e) {
        var preventKeyPress;
        if (e.keyCode == 8) {
            var d = e.srcElement || e.target;
            switch (d.tagName.toUpperCase()) {
                case 'TEXTAREA':
                    preventKeyPress = d.readOnly || d.disabled;
                    break;
                case 'INPUT':
                    preventKeyPress = d.readOnly || d.disabled ||
                        (d.attributes["type"] && $.inArray(d.attributes["type"].value.toLowerCase(), ["radio", "checkbox", "submit", "button"]) >= 0);
                    break;
                case 'DIV':
                    preventKeyPress = d.readOnly || d.disabled || !(d.attributes["contentEditable"] && d.attributes["contentEditable"].value == "true");
                    break;
                default:
                    preventKeyPress = true;
                    break;
            }
        }
        else
            preventKeyPress = false;

        if (preventKeyPress)
            e.preventDefault();
    });
   
Comments
BillWoodruff 8-Feb-15 4:23am
   
+5 good answer !
C#
$(document).keydown(function(e){
    if ( e.keyCode == 8 ) e.preventDefault();
});
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900