Check caps lock – javascript

Code below checks if Caps lock key is on and gives an alert while typing in some text or password field. see example below:

 
function checkCapsLock( e ) {
	var keyCode=0;
	var shiftKey=false;
	var msg='Gotcha!! Caps Lock is On.\nTo prevent entering your password incorrectly,\nyou should press Caps Lock to turn it off.';
	// Internet Explorer 4+
	if ( document.all ) {
		keyCode=e.keyCode;
		shiftKey=e.shiftKey;
 
	// Netscape 4
	} else if ( document.layers ) {
		keyCode=e.which;
		shiftKey=( keyCode == 16 ) ? true : false;
 
	// Netscape 6
	} else if ( document.getElementById ) {
		keyCode=e.which;
		shiftKey=( keyCode == 16 ) ? true : false;
 
	}
 
	// Upper case letters are seen without depressing the Shift key, therefore Caps Lock is on
	if ( ( keyCode >= 65 && keyCode <= 90 ) && !shiftKey ) {
		alert( msg );
 
	// Lower case letters are seen while depressing the Shift key, therefore Caps Lock is on
	} else if ( ( keyCode >= 97 && keyCode <= 122 ) && shiftKey ) {
		alert( msg );
 
	}
}
 



– html –

 
<form>
<strong>Password:</strong>
<input type="Password" name="Password" size="16" maxlength="16" onkeypress="checkCapsLock( event )">
<p>
<input type="Reset">
</form>
 


Password:

Most Commented Posts

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

nice thing but it does work i copy paste text through mouse..!!! :P :P

Leave a comment

(required)

(required)