2

I have 2 challenges,

  1. On page load, I would like to show a warning saying that "caps is on".

  2. Still the user press the shift + key or Caps on + key - I would like to through error.

I search in google and find a solution, but it doesn't work. any one help me to sort this issue please? please refresh the page using caps lock on/off to get the current status. nothing works.

Here is the function I got from google search:

function capLock(e){
 kc = e.keyCode?e.keyCode:e.which;
 sk = e.shiftKey?e.shiftKey:((kc == 16)?true:false);
 if(((kc >= 65 && kc <= 90) && !sk)||((kc >= 97 && kc <= 122) && sk))
  console.log('caps lock on')
 else
  console.log('caps lock off');
}

$('#password').on('keypress', function(e){
    capLock(e.which)
})

online demo

Bhushan Kawadkar
  • 28,279
  • 5
  • 35
  • 57
3gwebtrain
  • 14,640
  • 25
  • 121
  • 247
  • Can't be detected, only heuristically guessed at. – Amadan Jun 04 '14 at 05:58
  • there is no way to find the caps is on or off while load the page?. Actually the answers what on the question all was tried. problem is there is no proper deduction for on page load again mixing cpas+shit and shif+char and caps+shift+char - is not working properly. – 3gwebtrain Jun 04 '14 at 06:01

3 Answers3

0

Demo Don't pass e.which .you have to pass event

 function capLock(e) {
        kc = e.keyCode ? e.keyCode : e.which;
        sk = e.shiftKey ? e.shiftKey : ((kc == 16) ? true : false);
        if (((kc >= 65 && kc <= 90) && !sk) || ((kc >= 97 && kc <= 122) && sk)) console.log('caps lock on')
        else console.log('caps lock off');
    }

    $('#password').on('keypress', function (e) {
        capLock(e)
    });
Balachandran
  • 9,567
  • 1
  • 16
  • 26
0

Actually you are passing e.which instead of e while calling capLock function.

Try this : Pass only event e while calling capLock function.

$('#password').on('keypress', function(e){
    capLock(e);
})

Working JSFiddle

Bhushan Kawadkar
  • 28,279
  • 5
  • 35
  • 57
0

I used a new approach to find the letters on key press: here is my solution:

    $('#password').keypress(function(e) { 
    var Caps = null;
    var s = String.fromCharCode( e.which );
    if ( s.toUpperCase() === s && s.toLowerCase() !== s && !e.shiftKey ) {
        Caps = true;
    } else {
        Caps = false;
    }

    if(s.toUpperCase() === s){
        Caps = true;
    }else{
        Caps = false;
    }

    if(Caps){
        console.log('no use of the caps letter');
    }
});

any comment or correction or update.. expected. thanks

3gwebtrain
  • 14,640
  • 25
  • 121
  • 247