admin管理员组文章数量:1389768
I have this code for adding a keypress to a password. If capslock is on it will trigger. I got it from How do you tell if caps lock is on using JavaScript?
$("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 original code has the message in a span
:
...}).after(<span class='capsWarn error' style='display:none;'>CAPSLOCK is ON</span>);
I wanted it to be an alert message but it is not performing as I expect it to. On load it should alert the message but even if the capslock
is on
it is not showing the alert.
How do I get it to detect the key and present the alert?
I have this code for adding a keypress to a password. If capslock is on it will trigger. I got it from How do you tell if caps lock is on using JavaScript?
$("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 original code has the message in a span
:
...}).after(<span class='capsWarn error' style='display:none;'>CAPSLOCK is ON</span>);
I wanted it to be an alert message but it is not performing as I expect it to. On load it should alert the message but even if the capslock
is on
it is not showing the alert.
How do I get it to detect the key and present the alert?
Share Improve this question edited May 23, 2017 at 12:26 CommunityBot 11 silver badge asked Jun 10, 2015 at 3:31 guradioguradio 15.6k4 gold badges38 silver badges64 bronze badges1 Answer
Reset to default 6$("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');
}
本文标签: javascriptDetecting when CAPS LOCK is ONStack Overflow
版权声明:本文标题:javascript - Detecting when CAPS LOCK is ON - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744690073a2619940.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论