Copy the code code as follows:
//Match Chinese numbers, letters and underscores
var checkInput = function (str) {
var pattern =var pattern = /^[/w/u4e00-/u9fa5]+$/gi;
if(pattern.test(c))
{
return false;
}
return true;
}
1. Use regular expressions to filter special characters in js and verify whether all input fields contain special symbols.
Copy the code code as follows:
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
Copy the code code as follows:
/**
* Verify whether all input fields contain special symbols
* The symbols to be filtered are written 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 special symbols to be filtered here
//Note: Modify the characters at ####, and other parts are not allowed to be modified.
//if(/^[^####]*$/.test(form.elements[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 /n/n 1 single quotation mark: ' /n 2 double quotation mark: /" /n 3 vertical bar: | /n 4 sharp angle: < > /n/nPlease check your input!" );
return false;
}
}