Detection when CAPS LOCK is enabled
I have this code to add a password push. If capslockenabled, it will fire. I got it from How do you know if JavaScript blocking is enabled?
$("input[type='password']").keypress(function(e) {
var $warn = $(this).next(".capsWarn"); // handle the warning mssg
var kc = e.which; //get keycode
var isUp = (kc >= 65 && kc <= 90) ? true : false; // uppercase
var isLow = (kc >= 97 && kc <= 122) ? true : false; // lowercase
// event.shiftKey does not seem to be normalized by jQuery(?) for IE8-
var isShift = ( e.shiftKey ) ? e.shiftKey : ( (kc == 16) ? true : false ); // shift is pressed
// uppercase w/out shift or lowercase with shift == caps lock
if ( (isUp && !isShift) || (isLow && isShift) ) {
$warn.show();
} else {
$warn.hide();
}
}).after(capLock());
function capLock(e){
alert('CAPSLOCK is ON');
}
The source code has a message in span
:
...}).after(<span class='capsWarn error' style='display:none;'>CAPSLOCK is ON</span>);
I wanted this to be a warning message, but it doesn't get executed as I expect. When loading it should warn about it, but even if capslock
is on
, it is not showing a warning.
How do I get it to detect the key and present an alert?
+3
source to share
1 answer
$("input[type='password']").keypress(function(e) {
var $warn = $(this).next(".capsWarn");//can be removed since you are just using alert
var kc = e.which; //get keycode
var isUp = (kc >= 65 && kc <= 90) ? true : false; // uppercase
var isLow = (kc >= 97 && kc <= 122) ? true : false; // lowercase
// event.shiftKey does not seem to be normalized by jQuery(?) for IE8-
var isShift = ( e.shiftKey ) ? e.shiftKey : ( (kc == 16) ? true : false ); // shift is pressed
// uppercase w/out shift or lowercase with shift == caps lock
if ( (isUp && !isShift) || (isLow && isShift) ) {
capLock(); // alerts "CAPSLOCK is ON"
}
});
function capLock() {
alert('CAPSLOCK is ON');
}
+6
source to share