Home >Web Front-end >JS Tutorial >Sample code for js special character filtering_javascript skills

Sample code for js special character filtering_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:57:111030browse

Copy code The code is as follows:

//Match Chinese digits, letters and underscores 
var checkInput = function (str) {
      var pattern = var pattern = /^[wu4e00-u9fa5] $/gi;
                                                                                                                                                                                                                               > }
}
return true;
}


1.js uses regular expressions to filter special characters and verify whether all input fields contain special symbols


function stripscript(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;
}


2. Verify whether all input fields contain special symbols


/**
* Verify whether all input fields contain special symbols
* Write the symbols to be filtered into the regular expression. Note that some symbols need to be escaped with ''.
* Test example:
* if(checkAllTextValid(document.forms[0]))
* alert("All text boxes in the form passed verification!");
*/
function checkAllTextValid(form) {
//Record the number of text boxes without quotation marks
var resultTag = 0;
//Record the number of all text text boxes
var flag = 0;
for (var i = 0; i < form.elements.length; i ) {
if (form.elements[i].type == "text") {
flag = flag 1;
//Fill in the required filter here Special symbols
                                                                          ’’ ’ ’ ’s ’ ’ s ’ ’ s ’ ’ s ’ ’ s ‐ ‐ ‐ ‐ ‐ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ‐ ‐ ​ ​ ​ ​i].value))

If (/^[^|"'<>]*$/.test(form.elements[i].value))
resultTag = resultTag 1;
else
form.elements[i].select();
}
}

/**
* If the text box containing quotes is equal to the value of all text boxes, the verification passes
*/
if (resultTag == flag )
return true;
else {
alert("The text box cannot contain nn 1 single quotes: ' n 2 double quotes: " n 3 vertical bars: | n 4 sharp angles: < > nnPlease check your input! ");
return false;
}
}


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