• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4270
  • Last Modified:

Caps lock check with Javascript in Firefox

I found the following JavaScript code which is exactly what I was looking for in another question.  However the only problem is the shift key check does not work in Firefox, does anyone know what code needs to be added to get this to work in Firefox.

<TITLE>Check Caps Lock</TITLE>
<LINK REL="stylesheet" HREF="jgw.css" TYPE="text/css">

// ///////////////////////////
// checkCapsLock v1.0
// If a user enters his password in a Web-based form with Caps Lock
// accidentally on, he can become frustrated because his password is not
// being accepted... and he may assume the application is the source of the
// problem.
// This JavaScript function will let the user know his Caps Lock is on and
// about the potential for error.
// ///////////////////////////

function checkCapsLock( e ) {

      var myKeyCode=0;
      var myShiftKey=false;
      var myMsg='Caps Lock is On.\n\nTo prevent entering your password incorrectly,\nyou should press Caps Lock to turn it off.';

      // Internet Explorer 4+

      if ( document.all ) {

      // Netscape 4
      } else if ( document.layers ) {
            myShiftKey=( myKeyCode == 16 ) ? true : false;

      // Netscape 6
      } else if ( document.getElementById ) {
            myShiftKey=( myKeyCode == 16 ) ? true : false;


      // Upper case letters are seen without depressing the Shift key, therefore Caps Lock is on
      if ( ( myKeyCode >= 65 && myKeyCode <= 90 ) && !myShiftKey ) {
            alert( myMsg );
            return false;

      // Lower case letters are seen while depressing the Shift key, therefore Caps Lock is on
      } else if ( ( myKeyCode >= 97 && myKeyCode <= 122 ) && myShiftKey ) {
            alert( myMsg );
            return false;

// -->


<H1>Check Caps Lock</H1>

Do you have problems with users who continually have problems logging in
because they inadvertently have their Caps Lock on?  Well, now you can let
your users know the problem may be due to Caps Lock.

<FORM name=pwform>
<INPUT TYPE="Text" NAME="Password" SIZE=16 MAXLENGTH=16 onKeyPress="return checkCapsLock(event)">
<INPUT TYPE="Reset">


1 Solution
You may refer to this website for the details of keystrokes detection for different browser.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now