views:

375

answers:

1

I was wondering if someone could help me clean up this code if possible; including merging the two functions into one. I'm new to javascript so I could really use some help.

I was able to get this script working quite well, I just want to know if it can be refined and/or if there are any problems with it that I overlooked.

The script first determines whether caps lock is on or off based on keypresses. Once caps lock state is determined, pressing the caps lock key simply toggles the caps lock warning message on or off.

Thanks,

Jeff

<script language="javascript">

//Caps Lock Warning
var onOff = "";
function capLock(e){
var kc = e.keyCode?e.keyCode:e.which;
var sk = e.shiftKey?e.shiftKey:((kc == 16)?true:false);
var cl = document.getElementById('cl');

if(onOff == ""){

//Checks if caps lock is on.
if(((kc >= 65 && kc <= 90) && !sk)||((kc >= 97 && kc <= 122) && sk))
{
cl.style.visibility = 'visible'; 
onOff = "on"; 
} 
//Checks if caps lock is off.    
else if(((kc >= 65 && kc <= 90) && sk)||((kc >= 97 && kc <= 122) && !sk))
{
cl.style.visibility = 'hidden'; 
onOff = "off"; 
}  
else
{
cl.style.visibility = 'hidden';  
}
}  
};

//Hides/shows Caps Lock warning when Caps Lock key is pressed once Caps Lock
//state is determined.
function hideMsg(e){
var cl = document.getElementById('cl');    
var KeyID = (window.event) ? event.keyCode : e.keyCode; 

if(KeyID==20 && onOff == "on")
{
cl.style.visibility = 'hidden';
onOff = "off";
}
else if(KeyID==20 && onOff == "off")
{
cl.style.visibility = 'visible';
onOff = "on";
}
}
document.onkeydown=hideMsg

</script>


<body OnLoad="document.form1.tb1.focus();">
<form name="form1">
<input name="tb1" onkeypress="capLock(event)" type="text" onLoad="javascript:this.focus" /><p />
<input name="tb2" onkeypress="capLock(event)" type="password" /><p />
<input id="Reset1" type="reset" value="reset" /><p />
<span id="cl" class="hint" style="visibility:hidden;">Caps Lock is On!
<p>Having Caps Lock on may cause you to enter your password incorrectly.</p>
<p>You should press Caps Lock to turn it off before entering your password.<p/>
</span>
</form>
</body>
+1  A: 

I would think in a re-write...

But first, how do you detect if CapsLock is on?

You can inspect the pressed keys, and it will be on if:

  • The pressed key is uppercase, and shift is not pressed, or
  • The pressed ket is lowercase, and shift is pressed

Something simple like this would work:

(function () {
  var hint = document.getElementById('cl');

  var keypressHandler = function (e) {
    e = e || window.event;
    var keyCode =  e.keyCode || e.which,
    character = String.fromCharCode(keyCode),
    isLetter = /[a-z]/i.test(character), 
    capsLockOn = isLetter && (character === character.toUpperCase() && !e.shiftKey) || 
    // Case 1: capslock on, without shift pressed
                (character === character.toLowerCase() && e.shiftKey);
    // Case 2: capslock on, with shift pressed

    hint.style.display = capsLockOn ? '' : 'none'; // show or hide the 'hint'
  }

  // Bind keypress event to both inputs:
  document.getElementById('tb1').onkeypress = 
  document.getElementById('tb2').onkeypress = keypressHandler;
})();

Check an example of the above code with your markup here.

CMS
Jeff