Using the KeyDown event to prevent users from entering implementation code

Source: Internet
Author: User
Tags numeric lowercase

  This article mainly introduces the use of KeyDown events to prevent user input concrete implementation, the need for friends can refer to the following

Understand the difference between the events of the next     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: Occurs when a key is released when the control has focus     1, KeyPress is mainly used to receive letters, numbers and other ANSI characters. KeyDown and KeyUP event procedures can usually capture the keyboard except PRSCRN all keys (Special keys     2 for special keyboards are not discussed here, KeyPress can only capture single characters, KeyDown and KeyUP capture key combinations.)     3, KeyPress does not display the physical state of the keyboard (Shift key), but simply passes a character. KeyPress interprets the large and lowercase forms of each character as a different key code, that is, as two different characters. KeyDown and KeyUp cannot determine the size of key-value letters. KeyDown and KeyUp interpret the uppercase and lowercase forms of each character with two parameters: keycode-displays the physical keys (returns a and a as the same key) and shift-indicates the state of the SHIFT + key key and returns either A or a.     5, KeyPress does not distinguish between keypad and main keyboard numeric characters, KeyDown and KeyUp distinguish between keypad and main keyboard numeric characters.     6, KeyDown, KeyUp events occur when a key is pressed (KeyDown) or released (KEYUP). Because the keyboard usually press the key will be immediately released (this is different from the mouse), so the two events using which difference is not. Also, the up and the other two have a difference: to determine the state of the key modified must use up.     We can use the KeyDown event to block user input, such as an input field can only enter the number     keyboard number keys keycode    [48-57] number key   [96-105] Numeric keypad   also allows backspace key deletion     code as follows     code as follows: var input = document.getElementById (' Number_ipt ')   Input.onkeydown = function (e) {  var keycode = e.keycode  if (!isnumber (keycode)) return false }   //Only numeric   function isnumb ER (keycode) { //Digital   if (keycode >= && keycode <=) return true //small numeric keypad   if (ke Ycode >= && keycode <=) return true //backspace key   if (KeyCode = 8) return true  RET Urn false }   

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.