Javascript prohibits the back key of Backspace instance code

  • 2020-03-29 23:46:02
  • OfStack


function banBackSpace(e) {  
                var ev = e || window.event;//Gets the event object & cake;
                var obj = ev.target || ev.srcElement;//Getting the source of the event & NBSP;
                var t = obj.type || obj.getAttribute('type');//Gets the event source type & NBSP;
                //Gets the type of event & NBSP; that is a judgment condition;
                var vReadOnly = obj.readOnly;  
                var vDisabled = obj.disabled;  
                //Dealing with the undefined situation & sponge;
                vReadOnly = (vReadOnly == undefined) ? false : vReadOnly;  
                vDisabled = (vDisabled == undefined) ? true : vDisabled;  
                //When the Backspace key is struck, the event source type is password or single-line, multi-line text, & NBSP;
                //If the readOnly property is true or the disabled property is true, the backspace key is invalid & PI.
                var flag1 = ev.keyCode == 8 && (t == "password" || t == "text" || t == "textarea") && (vReadOnly == true || vDisabled == true);  
                //When the Backspace key is struck, the Backspace key is invalidate if the event source type is not password or if it is a single line, multi-line text.
                var flag2 = ev.keyCode == 8 && t != "password" && t != "text" && t != "textarea";  
                //Judge  
                if (flag2 || flag1) return false  

            }  
            //Disable the backspace key for Firefox, Opera 
            document.onkeypress = banBackSpace;  
            //Disable backspace key on IE, chrome & cake;
            document.onkeydown = banBackSpace;  

Related articles: