Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 单击按钮时,阻止文本框中所有不合适的单词_Javascript_Html - Fatal编程技术网

Javascript 单击按钮时,阻止文本框中所有不合适的单词

Javascript 单击按钮时,阻止文本框中所有不合适的单词,javascript,html,Javascript,Html,下面是我的代码,当我在文本框中插入时,它显示了插入kill、fight、slap的通知。 但是我想阻止文本框中可能出现的所有不合适的词,比如f**k等等。你们有什么想法吗。谢谢 <html> <head> <meta charset="utf-8" /> <title></title> </head> <body&g

下面是我的代码,当我在文本框中插入时,它显示了插入kill、fight、slap的通知。 但是我想阻止文本框中可能出现的所有不合适的词,比如f**k等等。你们有什么想法吗。谢谢

       <html>  
       <head>  
       <meta charset="utf-8" />  
       <title></title>  
       </head>  
       <body>  
       <div id="wrapper" style="width:600px; margin:0 auto;">  
        <h2></h2>  
        <input id="txtWords" style="width:300px;" />  

        <br />  
        <input type="button" id="btnCheck" onclick="fnCheckForRestrictedWords();" value="Check For 
       Restricted Words">  
        </div>  
        <script type="text/javascript">  
        function fnCheckForRestrictedWords() {  
            var restrictedWords = new Array("kill", "fight", "slap");  
            var txtInput = document.getElementById("txtWords").value;  
            var error = 0;  
            for (var i = 0; i < restrictedWords.length; i++) {  
                var val = restrictedWords[i];  
                if ((txtInput.toLowerCase()).indexOf(val.toString()) > -1) {  
                    error = error + 1;  
                }  
            }  

            if (error > 0) {  
                alert('You have entered some restricted words.')  
            }  
            else {  
                // Your logic here  
            }  
         }  
        </script>  

       </body>  
      </html>  


函数fnCheckForRestrictedWords(){ var restrictedWords=新数组(“杀死”、“战斗”、“拍击”); var txtInput=document.getElementById(“txtWords”).value; var误差=0; 对于(var i=0;i-1{ 误差=误差+1; } } 如果(错误>0){ 警报('您输入了一些限制词') } 否则{ //你的逻辑在这里 } }

includes()
方法应该可以工作:txtInput.includes(restrictedWords[i]),它返回true或false。您还希望包含更多的坏单词和不同的书写方式

您需要定义所有“坏”单词并将其放入黑名单。您可以使用一些现有列表作为列表的起点:

资料来源:


如果您想包括模糊的“坏”字,您可能还需要将添加到列表中

原则上这是不可能的。有太多的单词和不同的书写方式,或添加标点符号等。但有一个名为“坏单词”的软件包可能会帮助您: