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
- WBOYOriginal
- 2016-05-16 18:26:212363browse
Code 1: Use when keypress event >/****************************************************/
//Function: Filter illegal characters
/ /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
//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 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);
}
Statement: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