JavaScript uses keyboard input control to implement digital verification function _javascript Skills

Source: Internet
Author: User

Now there is a demand for the following figure:

Product manager said card number can only let input numbers (the middle of the space is the format of the added, but also with JS), sometimes my mind appeared a voice, what things, add a type=number not on the line, the fact that the pattern Tucson broken, not to say type= Number will be followed by a subscript (although using CSS to kill), but this type is to support the scientific input method, is the decimal point and e Such is can input, so can only be used in other ways, and then want to use to retrieve the input is not a number to kill, but this can still input, the idea was called back, The final adoption of the keyboard input control method, in fact very simple, the code is as follows:

  var isnumber=function (keycode) {
        //digital
        if (keycode >= && keycode <=) {return
          true;  
        }
          //Small numeric keypad
        if (keycode >= && keycode <=) {return
          true;
        }
          Tab Backspace, Del, left and RIGHT arrow keys
        if (keycode = = 9| | KeyCode = = 8| | KeyCode = 32 | | keycode = 46 | | keycode = 37 | | KeyCode = =) {return
          true;
        }
        return False
      }

The so-called keyboard input control is based on the keyboard input of the keycode to determine what type of input, that is, to get the keyboard keys in the key to Unicode value, the keyboard on each key corresponding to the keycode is fixed, there are many tables on the Internet, I do not put up here, Each button to monitor the corresponding keycode and then check whether it is legitimate, it is necessary to note that here is not only the legal number, there are up and down the tab and Space direction key, can not disable the user's operation instructions, how, looks very simple.

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.