Now there is a requirement as shown below:
The product manager said that Card Number can only input numbers (the spaces in the middle are added by the format and are also implemented by js). Sometimes a sound appears in my mind. What's the matter? I just add type=number. In fact, I found that the drawing is broken. I won't say that there will be an upper and lower mark after type=number (although it can be killed with css), but this type supports scientific input methods, which are decimal points and e that can be input, so I can only use other methods. Later, I wanted to use the search to kill it if I entered a non-number, but I can still input it, and the idea is called back. So I finally adopted the keyboard input control method. It is actually very simple, the code is as follows:
var isNumber=function(keyCode){ // Number if (keyCode >= 48 && keyCode <= 57) { return true; } // Small numeric keyboard if (keyCode >= 96 && keyCode <= 105) { return true; } //tab Backspace, del, left and right arrow keys if (keyCode == 9||keyCode == 8|| keyCode == 32 || keyCode == 46 || keyCode == 37 || keyCode == 39) { return true; } return false }The so-called keyboard input control is to determine the type of input based on the keycode input on the keyboard, that is, to obtain the Unicode value of the keyboard key pressed. The keycode corresponding to each key on the keyboard is fixed. There are many comparison tables on the Internet, so I won’t post them here. Every time the key presses monitor the corresponding keycode and then compare whether it is legal. It should be noted that the legal ones here are not only the numbers, but also the up, down, left and right tabs and space arrow keys. User operation instructions cannot be disabled. How about it, it looks very simple.