Home > Web Front-end > JS Tutorial > Using the Keydown event to prevent users from entering implementation code_javascript skills

Using the Keydown event to prevent users from entering implementation code_javascript skills

WBOY
Release: 2016-05-16 16:55:41
Original
2048 people have browsed it

First understand the difference between each event

KeyDown: Occurs when a key is pressed when the control has focus
KeyPress: Occurs when a key is pressed when the control has focus
KeyUp: In Occurs when the key is released when the control has focus

1. KeyPress is mainly used to receive ANSI characters such as letters and numbers. The KeyDown and KeyUP event processes can usually capture all keys on the keyboard except PrScrn (special keys of special keyboards are not discussed here

2. KeyPress can only capture a single character, while KeyDown and KeyUp can capture key combinations.

3. KeyPress does not display the physical status of the keyboard (SHIFT key), but only passes a character. KeyPress interprets the upper and lower case forms of each character as different key codes, that is, KeyDown and KeyUp cannot be used. Determine the size of the key's letters. KeyDown and KeyUp interpret the uppercase and lowercase forms of each character with two parameters: keycode — which displays the physical key (returns A and a as the same key) and shift — which indicates the shift of the key. Status and returns one of A or a.

5. KeyPress does not distinguish between the numeric characters of the small keyboard and the main keyboard. KeyDown and KeyUp distinguish the numeric characters of the small keyboard and the main keyboard.

6. , KeyDown, KeyUp events occur when a key is pressed (KeyDown) or released (KeyUp). Since generally pressed keyboard keys are often released immediately (this is different from the mouse), which one to use for these two events. The difference is not big. Moreover, there is another difference between up and the other two: to determine the modified state of the key, you must use up. We can use the keydown event to prevent the user from inputting. For example, a certain input field can only use up. Enter numbers

keyCode of the numeric keys on the keyboard

[48-57] numeric keys
[96-105] numeric keypad
In addition, the Backspace key is allowed to delete

The code is as follows


var input = document.getElementById(' number_ipt')
input.onkeydown = function(e) {
var keyCode = e.keyCode
if ( !isNumber(keyCode) ) return false
}

// only Can input numbers
function isNumber(keyCode) {
// Number
if (keyCode >= 48 && keyCode <= 57 ) return true
// Small numeric keyboard
if ( keyCode >= 96 && keyCode <= 105) return true
// Backspace key
if (keyCode == 8) return true
return false
}

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template