others - Javascript - 在文本框中,屏蔽所有不適當的單詞

謝謝


 <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> 



时间:

這個

 
includes()



方法應該有效:txtInput.includes(restrictedWords [i] )返回true或false。

...