Script (JS) control page Input

Source: Internet
Author: User
Brief Introduction:

The events in IE have many keycodes, And the keycodes of the same key in different events (onkeydown, onkeypress, onkeyup) may also be different.

Onkeydown event fires when the user presses a key.
For case insensitive keycode, the keycode for A-z/A-Z is 65-90
Keycode is differentiated between numbers and keypad numbers. The keycode of 0-9 is 48-57, and the number of 0-9 is 96-105.

Onkeypress event fires when the user presses an alphanumeric key.
Opposite to onkeydown
For case-sensitive keycode, the keycode for A-z/A-Z is 65-90
Keycode is differentiated between numbers and keypad numbers. The keycode of 0-9 is 48-57, and the number of 0-9 is 96-105.

Onkeyup event fires when the user releases a key.

1. Only letters and numbers can be entered, and lowercase letters can be converted into uppercase letters.
<Script language = "JavaScript">
Function checkchar (){
If (event. keycode <48 & event. keycode! = 45)
| (Event. keycode> 57 & event. keycode <65)
| (Event. keycode> 90 & event. keycode <97)
| Event. keycode> 122 ){
Event. returnvalue = false;
}
If (event. keycode >=97 & event. keycode <= 122 ){
Event. keycode-= 32;
}
}

// This method cannot prevent users from copying Chinese characters or Inputting Chinese characters using the Chinese Input Method
// Check the value of the text and perform case conversion only when submitting the text.
Function check (){
VaR objval = Document. All ("name"). value;
VaR charcode = "";
VaR returnobj = "";
For (VAR I = 0; I <objval. length; I ++ ){
Charcode = objval. charcodeat (I );
If (charcode <48 & charcode! = 45)
| (Charcode> 57 & charcode <65)
| (Charcode> 90 & charcode <97)
| Charcode> 122 ){
Alert ("'" + objval. charat (I) + "' is invalid. Enter a letter, number, or hyphen -! ");
Return false;
}
If (charcode> = 97 & charcode <= 122 ){
Charcode-= 32;
}
Returnobj + = string. fromcharcode (charcode );
}
Document. All ("name"). value = returnobj;
Return true;
}
</SCRIPT>

<Input type = "text" name = "name" onkeypress = "checkchar ()" onpaste = "Return false;">
<Input type = "button" name = "BTN" onclick = "check ()">

2. The following are the reprinted items on the Internet.CodeIf you think it is better, you can extract it.
<Script language = JScript>
Function number (){
VaR char = string. fromcharcode (event. keycode );
Alert ("number:" + char );
VaR Re =/[0-9]/g;
Event. returnvalue = Char. Match (re )! = NULL? True: false
}

Function filterinput (){
Alert ("filterinput:" + event. type );
If (event. type. indexof ("key ")! =-1 ){
VaR Re =/37 | 38 | 39 | 40/g
If (event. keycode. tostring (). Match (re) return false
}
Event. srcelement. value = event. srcelement. value. Replace (/[^ 0-9]/g ,"")
}

Function filterpaste (){
VaR OTR = this.doc ument. selection. createRange ();
VaR text = Window. clipboardData. getdata ("text ");
OTR. Text = text. Replace (/[^ 0-9]/g ,"");
}
</SCRIPT>

<Input onkeypress = "number ()" onkeyup = "filterinput ()" onchange = "filterinput ()" onbeforepaste = "filterpaste () "onpaste =" Return false "style =" ime-mode: Disabled ">

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.