Pages

Monday, March 19, 2012

Detect Caps Lock with 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('capsLockWarning').style.visibility = 'visible';
     else
      document.getElementById('capsLockWarning').style.visibility = 'hidden';
  }

Using the script:
<input type="password" name="password" size="20" onkeypress="capLock(event)"><span id="capsLockWarning" style="visibility:hidden"> Caps Lock is on. </span>

1 comment: