Code One: KeyPress Event use
Copy Code code as follows:
/****************************************************/
Function: Filter Illegal characters
Description: Use when KeyPress event
Author: XXXXXXX
Date: May 7, 2010
/****************************************************/
function Surnam_keypress (event) {
Illegal character Set
var codes = ' <>/@#% ';
Event
var e = Event | | Window.event
Print character codes
var code = E.charcode | | E.keycode;
Function key to return directly
if (E.charcode = = 0) return true;
CTR and Alt return directly
if (E.ctrlkey | | e.altkey) return true;
ASCII characters
if (Code <) return true;
Character code to character
var c = string.fromcharcode (code);
Do not print if you have illegal characters
if (Codes.indexof (c)!=-1) {
return false;
}
else {
return true;
}
}
code two Onchage (mainly when the user is pasting), KeyUp event
Copy Code code as follows:
/****************************************************/
Function: Filter Illegal characters
Description: Use when onchange, KeyUp events
Author: XXXXX
Date: May 7, 2010
/****************************************************/
function Surnam_keyup (text) {
Control values
var TextValue = Text.value;
Illegal character Set
var codes = ' <>/@#% ';
Illegal character array
var Codearray = Codes.split (');
Loop replaces illegal characters
for (i = 0; i < codearray.length; i++) {
while (Textvalue.indexof (Codearray[i])!=-1) {
TextValue = Textvalue.replace (Codearray[i], "");
}
}
Assign a value to a control again
Text.value = TextValue;
}
Use instance:
Copy Code code as follows:
///<summary>
///Add character filter to control JS
///</summary>
/// <param name= "text" ></param>
public void charillegalfilting (System.Web.UI.WebControls.TextBox text) br>{
//control content Change
text. attributes["onchange"] = "surnam_keyup (this);";
//Keyboard Popup event
text. attributes["onkeyup"] = "surnam_keyup (this);";
//Keyboard press event
text. attributes["onkeypress"] = "return surnam_keypress ();";
}
protected void Page_Load (object sender, EventArgs e)
{
//Add illegal character filter
Charillegalfilting (epnamet EXT);
}