When the user encounters a page that requires them to enter a password, are there any capital letters in the password? Is Caps Lock on? Wouldn't it be better if we could give users hints?
The following sample code gives the method, and the necessary instructions are commented in the code.
Question knowledge points: event.keyCode and event.shiftKey
Code
]
Original text: http ://www.blogjava.net/majianan/archive/2007/02/01/97284.html <script>
function detectCapsLock(e){
valueCapsLock = e.keyCode ? e.keyCode:e.which; // Caps Lock 是否打开
valueShift = e.shiftKey ? e.shiftKey:((valueCapsLock == 16 ) ? true : false ); // shift键是否按住
if (((valueCapsLock >= 65 && valueCapsLock <= 90 ) && ! valueShift) // Caps Lock 打开,并且 shift键没有按住
|| ((valueCapsLock >= 97 && valueCapsLock <= 122 ) && valueShift)) // Caps Lock 打开,并且按住 shift键
document.getElementById('capStatus').style.visibility = 'visible';
else
document.getElementById('capStatus').style.visibility = 'hidden';
/*
javascript中keyCode代码对应表
event.keyCode=32 空格
event.keyCode=13 回车
event.keyCode=27 Esc
event.keyCode=16) Shift
event.keyCode=17) Ctrl
event.keyCode=18) Alt
*/
}
</script>