Click here to Skip to main content
Click here to Skip to main content

Detect Caps Lock with JavaScript

By , 16 Jan 2007
 

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)

About the Author

benjarras
Web Developer Softtek GDC Ensenada
Mexico Mexico
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionIE-Onkeypressmembertpkpradeep26 Dec '12 - 23:51 
GeneralMy vote of 4memberThe nk5 Oct '11 - 1:32 
GeneralMy vote of 5membersivakumar32126 May '11 - 2:48 
GeneralthanksmemberAkash442 Sep '10 - 18:16 
Generalrealy helpfulmemberAbhijit Jana10 Jun '08 - 4:05 
QuestionWhy?memberDomingo M. Asuncion13 Mar '07 - 16:11 
GeneralReally nice.memberumasankar312 Mar '07 - 17:34 
GeneralNice but..memberparadoxxl17 Feb '07 - 22:59 
QuestionIt doesn't work in IE6?memberbencalie30 Jan '07 - 14:10 
QuestionHow about non a-z characters?memberStaffan (xtaff) Sjöstedt22 Jan '07 - 22:22 
Thank you for pointing out this solution.
However, I have, along with a lot of others I think, problem with characters like äöå (I'm Swedish). The script does not react since they have a key code greater than 122.
 
So I suggest the following instead: Check if the last typed character is changed when transformed to lower/upper case depending on the shift key.
To achieve this we need to respond to onkeyup instead of onkeypress, because otherwise the input element will not have received the character yet.
 
So my suggestion goes like this:
 
<pre>
<script>
     function capLock(e){
     if( !e )
          e = window.event;
         
     kc = e.keyCode?e.keyCode:e.which;
     sk = e.shiftKey?e.shiftKeyCry | :(( kc == 16)?true:false);
    
     // get the src element
     var src = e.srcElement ? e.srcElement : e.target;
    
     // get the last character
     var c = src.value.substr( src.value.length-1,1 );
    
     // check upper/Lower case
     if(   c != c.toLowerCase() && !sk || c != c.toUpperCase() && sk )
        document.getElementById('divMayus').style.visibility = 'visible';
     else
        document.getElementById('divMayus').style.visibility = 'hidden';
 
    
     }
    
    
</script>
 
……
 

<input type="password" name="txtPassword" onkeyup="capLock(event)" />
<br>
<div id="divMayus" >Caps Lock is on.</div>
 
</pre>
 

Best regards
/Staffan
AnswerRe: How about non a-z characters?memberastanton19787 Mar '08 - 6:02 

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 16 Jan 2007
Article Copyright 2007 by benjarras
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid