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.