Home > Web Front-end > JS Tutorial > The front end of the webpage filters the code through js illegal characters (swearing words, etc.)_javascript skills

The front end of the webpage filters the code through js illegal characters (swearing words, etc.)_javascript skills

WBOY
Release: 2016-05-16 18:26:21
Original
2370 people have browsed it
Code 1: Use when keypress event >/****************************************************/
//Function: Filter illegal characters
//Description: Used during keypress event //Author: XXXXXXX //Date: May 7, 2010 /****************************************************/ function surnam_keypress(event) { //Illegal character setvar codes = '<>/@#%';
/ /Event
var e = event || window.event
//Print character code
var code = e.charCode || e.keyCode;
//Return directly when the function key is pressed
if (e.charCode == 0) return true;
//ctr and alt return directly
if (e.ctrlKey || e.altKey) return true;
//ASCII character
if (code < 32) return true;
//Convert character code to character
var c = String.fromCharCode(code);
//Do not print if there are illegal characters
if (codes .indexOf(c) != -1) {
return false;
}
else {
return true;
}
}



Code 2 onchage (mainly used for processing when users paste), keyup event





Copy code

The code is as follows:
/****************************************************/
//Function: Filter illegal characters
//Description: Used for onchange and keyup events //Author: XXXXX //Date: May 7, 2010/****************************************************/ function surnam_keyup(text) { //Control valuevar textvalue = text.value;
//Illegal character set
var codes = '<>/@#%';
//Illegal character array
var codearray = codes.split('');
// Loop to replace illegal characters
for (i = 0; i < codearray.length; i ) {
while (textvalue.indexOf(codearray[i]) != -1) {
textvalue = textvalue. replace(codearray[i], '');
}
}
//Reassign the value to the control
text.value = textvalue;
}



Usage example:





Copy code

The code is as follows:

/// /// Add character filtering js to the control
///
/// public void CharIllegalFilting(System.Web.UI.WebControls.TextBox text) { //Control content changestext.Attributes["onchange"] = "surnam_keyup(this);"; //Keyboard pop-up 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 filtering
CharIllegalFilting(epNametext);
}

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