JS implementation to determine if the CAPS lock is turned on

Source: Internet
Author: User

Reprint Address: http://www.cnblogs.com/xiaoao808/archive/2008/07/31/1257624.html

When a user logs in to enter a password, often there is a case of password error due to the opening of the Caps lock, if prompted when the user caps Lock is turned on, you can try to avoid the occurrence of this situation.

Here is the code:


<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 strict//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> detect CAPS LOCK key </title>
<style type= "Text/css" >
<!--
*{margin:0;padding:0;}
Body{padding:2em;background: #242424; color: #ccc;}
h1{text-align:center;line-height:200%;}
H3{text-indent:1em;line-height:160%;color: #666; font-weight:normal;font-size:1em;}
H3 A{color: #bbb; text-decoration:none;margin:0 0.5em;}
H3 A:hover{color: #fff;}
P{margin:5em;}
span{margin:0 0.5em;font-size:85.7%;}
-
</style>

<body>
<form action= "#" method= "POST" >
<p><label for= "password" > Password: </label><input type= "password" id= "password" name= "password"/> <span style= "Display:none;" > CAPS LOCK key is pressed, please note case </span></p>
</form>
<script type= "Text/javascript" >
<! [cdata[
function Detectcapslock (event) {
var e = event| | window.event;
var o = e.target| | E.srcelement;
var otip = o.nextsibling;
var keycode = e.keycode| | E.which; KeyCode of Keys
var isshift = E.shiftkey | | (keycode = = 16) | | false; The SHIFT key is pressed
if (
((keycode >= && keycode <=) &&!isshift)//Caps Lock turned on without holding down the SHIFT key
|| ((keycode >= && keycode <= 122) && isshift)//Caps Lock on, and hold down the SHIFT key
{oTip.style.display = ';}
Else{otip.style.display = ' None ';}
}
document.getElementById (' password '). onkeypress = Detectcapslock;
]]>
</script>
</body>

JS implementation to determine if the CAPS lock is turned on (go)

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.