JS Filter special character function to filter some illegal, there may be injected to the site browsing impact of special strings or characters Oh, we have two of these functions.
function Strips Tutorial Cript (s)
{
var pattern = new RegExp ("[' ~!@#$^&* () =|{} ':; '", [].<>/?~! @#¥......&* ()--| {}【】‘;:”“'。 ,、? ]")
var rs = "";
for (var i = 0; i < s.length; i++) {
rs = Rs+s.substr (i, 1). replace (pattern, ');
}
return RS;
}
Method Two
* Verify that all input fields contain special symbols
* The symbols to be filtered are written to the regular expression, noting that some symbols are "escaped".
* The characters to be escaped include: 1, Dot.
* 2, bracket []
* 3, curly brace {}
* 4, plus +
* 5, asterisk *
* 6, minus-
* 7, Slash
* 8, vertical bar |
* 9, Apex ^
* 10, Coins $
* 11, question mark?
* Sample:
* if (Checkalltextvalid (document.forms[0))
* Alert ("All text boxes in the form pass the checksum!") ");
*/
function Checkalltextvalid (form)
{
//record number of text boxes without quotes
var resulttag = 0;
//record number of all text boxes
var flag = 0;
for (var i = 0; i < form.elements.length i + +)
{
if (form.elements[i].type== "text")
{
Flagflag = flag + 1;
//Here fill in the special symbols to be filtered
Note: Change the characters at #, and other parts are not allowed to be modified.
//if (/^[^#]*$/.test (form.elements[i].value))
if (/^[^| "' <>]*$/.test (form.elements[i].value))
Resulttagresulttag = resulttag+1;
Else
Form.elements[i].select ();
}
}
/**
* If a text box with quotes is equal to the value of all text boxes, the checksum passes through the
*/
if (Resulttag = = flag)
return true;
Else
{
alert ("text box cannot contain nn 1 single quotes: ' N 2 double quotes:" N 3 Vertical bars: | N 4 Sharp Corner number: < > NN please check the input! ");
return false;
}
}