SoFunction
Updated on 2025-03-03

Sample code for special character filtering of js


/**
* Check whether all input fields contain special symbols
* The symbols to be filtered are written into the regular expression. Note that some symbols should be escaped with '\'.
* Test example:
 * if(checkAllTextValid([0]))
* alert("All text boxes in the form pass the verification!");
 */
function checkAllTextValid(form) {
//Record the number of text boxes without quotes
    var resultTag = 0;
//Record the number of all text boxes
    var flag = 0;
    for (var i = 0; i < ; i++) {
        if ([i].type == "text") {
            flag = flag + 1;
//Fill in the special symbols to be filtered here
//Note: When modifying the characters at ####, other parts are not allowed.
            //if(/^[^####]*$/.test([i].value))

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

    /**
* If the text box with quotes is equal to the value of all text boxes, the verification is passed
     */
    if (resultTag == flag)
        return true;
    else {
alert("The text box cannot contain \n\n 1 single quotes: ' \n 2 double quotes: \n 3 Vertical bars: | \n 4 sharp corners: < > \n\nPlease check input!");
        return false;
    }
}