Use Javascript to check whether the case-sensitive key is on

Source: Internet
Author: User

I just read a piece of code, I found that does not work, I use the MS IE6, but I did not open the capital key, press the letter also prompted

<Scr platinum pt>
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 ))
Document. getElementById ('divmayus'). style. visibility = 'visible ';
Else
Document. getElementById ('divmayus'). style. visibility = 'ddd ';
}
</Scr platinum pt>

<Input type = "password" name = "txtPassword" encrypted nkeypress = "capLock (event)"/>
<Div id = "divMayus" style = "visibility: hidden"> Caps Lock is on. </div>

Later, I suddenly thought of the toLowerCase () method. If the case key is on, the entered value must be different from the lower-case value. In this case, I don't know whether the upper-case key is on?

<Scr platinum pt>
Function capLock (){
Kc = event. srcElement. value;
If (kc! = Kc. toLowerCase ())
Document. getElementById ('divmayus'). style. visibility = 'visible ';
Else
Document. getElementById ('divmayus'). style. visibility = 'ddd ';
}
</Scr platinum pt>
<Input type = "test" name = "txtPassword" Export nkeyup = "capLock ()"/>
<Div id = "divMayus" style = "visibility: hidden"> Caps Lock is on. </div>

Two problems are found after writing. 1) the uppercase key is on, but if only a number is entered, no prompt is displayed. Fortunately, numbers are case-insensitive. 2) If you enter an uppercase letter, you will still be prompted even if the uppercase key is disabled, then I want to set only one variable to save the input, but it seems a lot of trouble.

Another way is to check the character entered at the end. The code hasn't been written yet. Let's talk about it later.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.