Wednesday, June 11, 2008

Detect Caps Lock with Javascript

clipped from www.codeproject.com
Detect Caps Lock with Javascript

Sometimes, when we want to 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 got an error with the username or password.

Solution

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

<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>
<SPAN><input type="password" name="txtPassword" onkeypress="capLock(event)" />
<SPAN><div id="divMayus" style="visibility:hidden">Caps Lock is on.</div>
 blog it

No comments: