Sample code for js special character filtering
- 2020-03-30 02:12:28
- OfStack
//Matches the Chinese alphanumeric underscore & NBSP;
var checkInput = function (str) {
var pattern =var pattern = /^[wu4e00-u9fa5]+$/gi;
if(pattern.test(c))
{
return false;
}
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("[`~!@#$^&*()=|{}':;',\[\].<>/?~ ! @# RMB... &* (a) — - |{} 【 】 '; : "" ' . ,,? ]")
var rs = "";
for (var i = 0; i < s.length; i++) {
rs = rs + s.substr(i, 1).replace(pattern, '');
}
return rs;
}
2. Verify that all input fields contain special symbols
function checkAllTextValid(form) {
//Records the number of text boxes without quotes
var resultTag = 0;
//Records the number of all 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 special symbol to be filtered here
//Note: modify the character at ####. Do not modify other parts.
//if(/^[^####]*$/.test(form.elements[i].value))
if (/^[^|"'<>]*$/.test(form.elements[i].value))
resultTag = resultTag + 1;
else
form.elements[i].select();
}
}
if (resultTag == flag)
return true;
else {
alert(" Text box cannot contain nn 1 Single quotes : ' n 2 Double quotation marks : " n 3 vertical bar : | n 4 Sharp corners, : < > nn Please check the input! ");
return false;
}
}