Jquery controls only numbers

Source: Internet
Author: User

You can only enter numbers in the control text box. For example, you can enter phone numbers and numbers. In this case, you must only enter numbers in the control text box. After JavaScript control is used, you cannot enter non-numeric keys on the keyboard in the English input method state. However, when you switch to the Chinese Input Method, you enter Chinese characters, press the space or enter key, and enter the Chinese or English letters. In this case, we need to disable the input method when getting the focus in the text box, that is, it does not respond to the switch of the input method. The following statement can implement this function.

The following statement is written using jquery. Only numbers and decimal places can be entered in the text box that controls the class as checknum.

JS Code
  1. // Listen to the keyboard. Only numbers and decimal places are allowed.
  2. $ (". Checknum"). keypress (function (event ){
  3. VaR keycode = event. Which;
  4. If (keycode = 46 | (keycode> = 48 & keycode <= 57 ))
  5. Return true;
  6. Else
  7. Return false;
  8. }). Focus (function (){
  9. This. style. imemode = 'Disabled ';
  10. });
// Listen to the keyboard. Only numbers and decimal places can be entered $ (". checknum "). keypress (function (event) {var keycode = event. which; If (keycode = 46 | (keycode >=48 & keycode <= 57) return true; elsereturn false ;}). focus (function () {This. style. imemode = 'Disabled ';});

Imemode has four forms:

Active indicates that the input method is Chinese.
Inactive indicates that the input method is in English.
Auto indicates that the input method is enabled (default)
Disable indicates that the input method is disabled.

Post original turn: http://haohaoxuexi.iteye.com/blog/1137834

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.