Web foreground through JS illegal character filter code (curse words, etc.) _javascript skills

Source: Internet
Author: User
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);
}

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.