Jquery converts uppercase and lowercase letters in the control text box

Source: Internet
Author: User

The form has special input requirements. The letters must be in uppercase or lowercase, such as the license plate number. You cannot force users to know that the CapsLock or Shift buttons are available. For better user experience, the control of the input box cannot be avoided. The following is jquery implementation.

$ ("# Id "). keypress (function (event) {if ($ (this ). val (). length <5) {// restrict the length of the input string var key = event. which; // event. keyCode if (key> = 97 & key <= 122) {// locate the range of the ascII code in which the input is a lowercase letter event. preventDefault (); // cancels the default event behavior $ (this ). val ($ (this ). val () + String. fromCharCode (key-32); // conversion} else {$ (this ). val ($ (this ). val ();} // control length });

The above can work properly in English input and output, but we generally use Chinese input methods, such as sogou. At this time, if you enter the characters, click Enter to enter the characters in English to the input box, the conversion does not occur. The implementation details of the specific causes will be detailed later. The alternative method is provided here, which is better than none.

Use the following code with the above Code colleagues to trigger the blur event after the cursor leaves the input box to implement case-insensitive conversion.

$ (Jid). blur (function () {$ (this). val ($ (this). val (). toUpperCase ());});

 

 

In this way, the case-insensitive conversion of text box input is basically realized.

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.