Click here to Skip to main content
Licence CPOL
First Posted 16 Jan 2007
Views 52,326
Bookmarked 17 times

Detect Caps Lock with JavaScript

By | 16 Jan 2007 | Article
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)

About the Author

benjarras

Web Developer
Softtek GDC Ensenada
Mexico Mexico

Member



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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 4 PinmemberThe nk1:32 5 Oct '11  
GeneralMy vote of 5 Pinmembersivakumar3212:48 26 May '11  
Generalthanks PinmemberAkash4418:16 2 Sep '10  
Generalrealy helpful PinmemberAbhijit Jana4:05 10 Jun '08  
QuestionWhy? PinmemberDomingo M. Asuncion16:11 13 Mar '07  
GeneralReally nice. Pinmemberumasankar317:34 12 Mar '07  
GeneralNice but.. Pinmemberparadoxxl22:59 17 Feb '07  
i've already seen something like that on this site http://www.mininova.org/register.php
QuestionIt doesn't work in IE6? Pinmemberbencalie14:10 30 Jan '07  
QuestionHow about non a-z characters? PinmemberStaffan (xtaff) Sjöstedt22:22 22 Jan '07  
AnswerRe: How about non a-z characters? Pinmemberastanton19786:02 7 Mar '08  

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.

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