SoFunction
Updated on 2025-03-03

JavaScript ban Backspace instance code


function banBackSpace(e) { 
var ev = e || ;//Get event object
var obj = || ;//Get the event source
var t = || ('type');//Get the event source type
//Get the event type as a judgment condition
                var vReadOnly = ; 
                var vDisabled = ; 
//handle undefined value situation
                vReadOnly = (vReadOnly == undefined) ? false : vReadOnly; 
                vDisabled = (vDisabled == undefined) ? true : vDisabled; 
//When the Backspace key is typing, the event source type is password or single-line or multi-line text,
//If the readOnly property is true or the disabled property is true, the backspace key will be invalid.
                var flag1 = == 8 && (t == "password" || t == "text" || t == "textarea") && (vReadOnly == true || vDisabled == true); 
//When the Backspace key is typing, if the event source type is not a password or single or multiple lines of text, the backspace key will be invalid.
                var flag2 = == 8 && t != "password" && t != "text" && t != "textarea"; 
//judge
                if (flag2 || flag1) return false 

            } 
//The backspace key is prohibited and acts on Firefox and Opera
            = banBackSpace; 
//The backspace key is prohibited and acts on IE and Chrome
            = banBackSpace;