Click here to Skip to main content
15,346,659 members
Articles / Programming Languages / Javascript
Posted 16 Jan 2007

Tagged as


17 bookmarked

Detect Caps Lock with JavaScript

Rate me:
Please Sign up or sign in to vote.
4.37/5 (13 votes)
16 Jan 2007CPOL
How to detect CapsLock with Javascript.


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.


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';
  document.getElementById('divMayus').style.visibility = 'hidden';

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.


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


About the Author

Web Developer Softtek GDC Ensenada
Mexico Mexico
No Biography provided

Comments and Discussions

SuggestionShould show message on caps lock change Pin
Arkadeep De3-Apr-15 7:16
professionalArkadeep De3-Apr-15 7:16 
QuestionIE-Onkeypress Pin
tpkpradeep26-Dec-12 23:51
Membertpkpradeep26-Dec-12 23:51 
GeneralMy vote of 4 Pin
the headlessnick5-Oct-11 1:32
Memberthe headlessnick5-Oct-11 1:32 
GeneralMy vote of 5 Pin
sivakumar32126-May-11 2:48
Membersivakumar32126-May-11 2:48 
Generalthanks Pin
Akash442-Sep-10 18:16
MemberAkash442-Sep-10 18:16 
Generalrealy helpful Pin
Abhijit Jana10-Jun-08 4:05
professionalAbhijit Jana10-Jun-08 4:05 
QuestionWhy? Pin
Domingo M. Asuncion13-Mar-07 16:11
MemberDomingo M. Asuncion13-Mar-07 16:11 
GeneralReally nice. Pin
umasankar312-Mar-07 17:34
Memberumasankar312-Mar-07 17:34 
GeneralNice but.. Pin
paradoxxl17-Feb-07 22:59
Memberparadoxxl17-Feb-07 22:59 
QuestionIt doesn't work in IE6? Pin
bencalie30-Jan-07 14:10
Memberbencalie30-Jan-07 14:10 
QuestionHow about non a-z characters? Pin
Staffan Sjöstedt22-Jan-07 22:22
MemberStaffan Sjöstedt22-Jan-07 22:22 
AnswerRe: How about non a-z characters? Pin
astanton19787-Mar-08 6:02
Memberastanton19787-Mar-08 6:02 

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

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