Javascript disables Backspace instance code

Source: Internet
Author: User

Copy codeThe Code is as follows:
Function banBackSpace (e ){
Var ev = e | window. event; // obtain the event object
Var obj = ev.tar get | ev. srcElement; // obtain the event Source
Var t = obj. type | obj. getAttribute ('type'); // obtain the event source type.
// Obtain the event type used as the judgment Condition
Var vReadOnly = obj. readOnly;
Var vDisabled = obj. disabled;
// Process the undefined Value
VReadOnly = (vReadOnly = undefined )? False: vReadOnly;
VDisabled = (vDisabled = undefined )? True: vDisabled;
// When you press the Backspace key, the event source type is password or single-line or multi-line text,
// If the readOnly attribute is true or the disabled attribute is true, the Return key is invalid.
Var flag1 = ev. keyCode = 8 & (t = "password" | t = "text" | t = "textarea ") & (vReadOnly = true | vDisabled = true );
// When you press the Backspace key, the Backspace key is invalid if the event source type is non-Password, single-line, or 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 and Opera
Document. onkeypress = banBackSpace;
// Disable the backspace key for IE and Chrome
Document. onkeydown = banBackSpace;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.