-1

I would like check if caps lock is on, but with my code i can check only the uppercases and not the caps lock.

jQuery('#password').keypress(function(e) {
        var s = String.fromCharCode( e.which );
        if ( s.toUpperCase() === s && s.toLowerCase() !== s && !e.shiftKey ) {
          jQuery('#caps').show();
        }
        else { 
          jQuery('#caps').hide();
        }
      });

this is my JS code.

Alessia
  • 59
  • 7
  • 1
    Does this answer your question? [How do you tell if caps lock is on using JavaScript?](https://stackoverflow.com/questions/348792/how-do-you-tell-if-caps-lock-is-on-using-javascript) – esqew May 06 '20 at 20:33
  • I tried it but it's the same, it checks only uppercases and not the caps lock – Alessia May 06 '20 at 20:35

1 Answers1

1

you can check like this using event.getModifierState("CapsLock"):-

var inputVal = document.getElementById("customInput");
var textMsg = document.getElementById("warningDiv");

inputVal.addEventListener("keyup", function(event) {

if (event.getModifierState("CapsLock")) {
    textMsg.style.display = "block";
  } else {
    textMsg.style.display = "none"
  }
});
#warningDiv{
  color: red;
  display: none;
}
<input type="text" id="customInput" />

<p id="warningDiv">WARNING! Caps lock is ON.</p>

you can check my solution here also- https://codepen.io/Arnab_Datta/pen/gOavXVJ?editors=1111

Arnab_Datta
  • 602
  • 7
  • 8