js过滤表单常规特殊字符

完整demo

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>demo</title>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
    <input type="text" id='i'>
    <script>
        function stripscript(s) {
            var pattern = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~!@#¥……&*()&mdash;—|{}【】‘;:”“'。,、?]")
                var rs = "";
            for (var i = 0; i < s.length; i++) {
                rs = rs + s.substr(i, 1).replace(pattern, '');
            }
            return rs;
        }
        $('#i').bind('input propertychange',function(){
            console.log($('#i').val());
            var val = $('#i').val();
            $('#i').val(stripscript(val));
        })
    </script>
</body>
</html>

 

posted @ 2020-04-14 16:54  expworld  阅读(436)  评论(0编辑  收藏  举报