Click here to Skip to main content
11,413,961 members (73,084 online)
Click here to Skip to main content

Detect Caps Lock with JavaScript

, 16 Jan 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
How to detect CapsLock with Javascript.

Introduction

I'm sure that this script will help you in some of your projects that needs a username and password.

Sometimes when we want access to a secure page that asks for a username and password and we submit the information but we didn't know that the password was submitted in upper case, we get an error.

Solution

We can avoid this kind of problems by just adding some JavaScript code to our page.

Building the script

<script language="Javascript">
function capLock(e){
 kc = e.keyCode?e.keyCode:e.which;
 sk = e.shiftKey?e.shiftKey:((kc == 16)?true:false);
 if(((kc >= 65 && kc <= 90) && !sk)||((kc >= 97 && kc <= 122) && sk))
  document.getElementById('divMayus').style.visibility = 'visible';
 else
  document.getElementById('divMayus').style.visibility = 'hidden';
}
</script>

Now we have our script ready to go. We now need to associate this script with our form.

Using the script

Let's add two items: a textbox and a DIV. Now we just need to call the onKeyPress event.

<input type="password" name="txtPassword" onkeypress="capLock(event)" />
<div id="divMayus" style="visibility:hidden">Caps Lock is on.</div> 

See if it works on your project.

License

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

Share

About the Author

benjarras
Web Developer Softtek GDC Ensenada
Mexico Mexico
No Biography provided

Comments and Discussions

 
SuggestionShould show message on caps lock change Pin
Arkadeep De, 3-Apr-15 8:16
professionalArkadeep De3-Apr-15 8:16 
QuestionIE-Onkeypress Pin
tpkpradeep, 27-Dec-12 0:51
membertpkpradeep27-Dec-12 0:51 
GeneralMy vote of 4 Pin
The nk, 5-Oct-11 2:32
memberThe nk5-Oct-11 2:32 
GeneralMy vote of 5 Pin
sivakumar321, 26-May-11 3:48
membersivakumar32126-May-11 3:48 
Generalthanks Pin
Akash44, 2-Sep-10 19:16
memberAkash442-Sep-10 19:16 
Generalrealy helpful Pin
Abhijit Jana, 10-Jun-08 5:05
memberAbhijit Jana10-Jun-08 5:05 
its realy helful ... Thanks for sharing Smile | :)

Best Regards
-----------------
Abhijit Jana
Check Out My Latest Article

Java.NET : Integration of Java and .NET[^]
"Success is Journey it's not a destination"

QuestionWhy? Pin
Domingo M. Asuncion, 13-Mar-07 17:11
memberDomingo M. Asuncion13-Mar-07 17:11 
GeneralReally nice. Pin
umasankar3, 12-Mar-07 18:34
memberumasankar312-Mar-07 18:34 
GeneralNice but.. Pin
paradoxxl, 17-Feb-07 23:59
memberparadoxxl17-Feb-07 23:59 
QuestionIt doesn't work in IE6? Pin
bencalie, 30-Jan-07 15:10
memberbencalie30-Jan-07 15:10 
QuestionHow about non a-z characters? Pin
Staffan (xtaff) Sjöstedt, 22-Jan-07 23:22
memberStaffan (xtaff) Sjöstedt22-Jan-07 23:22 
AnswerRe: How about non a-z characters? Pin
astanton1978, 7-Mar-08 7:02
memberastanton19787-Mar-08 7:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150427.2 | Last Updated 16 Jan 2007
Article Copyright 2007 by benjarras
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid