Block Bad Words Using JavaScript Validation

Tuesday, Mar 7, 2017

Tags:- HTML JavaScript


There are two kind of user one who write good words and other who write bad words.And to prevent this we use nothing but only use to delete comment or post after publish.So, in this tutorial we will show you how to block bad words using JavaScript Validation.You may also like block user from website using PHP.


See Demo
Block Bad Words Using JavaScript


To Block Bad Words It Takes Only One Step:-

  1. Make a HTML file and define markup and scripting


Step 1.Make a HTML file and define markup and scripting

We make a HTML file and save it with a name block.html

<html>
<head>
<script type="text/javascript">
function check_val()
{
 var bad_words=new Array("death","kill","murder");
 var check_text=document.getElementById("text").value;
 var error=0;
 for(var i=0;i<bad_words.length;i++)
 {
  var val=bad_words[i];
  if((check_text.toLowerCase()).indexOf(val.toString())>-1)
  {
   error=error+1;
  }
 }
	
 if(error>0)
 {
  document.getElementById("bad_notice").innerHTML="Some Bad Words In Your Text!";
 }
 else
 {
  document.getElementById("bad_notice").innerHTML="";
 }
}
</script>
</head>
<body>
<div id="wrapper">
 <textarea id="text" placeholder="Write Some Text Having Words 'death', 'kill', 'murder'"></textarea>
 <p id="bad_notice"></p>
 <input type="button" onclick="check_val();" value="CHECK WORDS">
</div>
</body>
</html>

In this step we create a textarea to write sample text and a button to check or validate bad words.We also create a check_val() function to validate bad words.In this function we made an array of bad words and then get sample text and find bad words in sample text using forloop we create a variable 'error' and set it to 0 and then in 'if' condition we first change the case of sample text to lowercase to find bad words accuartely and use indexOf to find bad words and if the value is greater than -1 it means one or more bad words is present in sample text then web increase the value of error by 1.And if the error value is greater than 0 then we print the error.You can use your own bad_words array.You may also like detect adblocker using JavaScript.


Thats all, this is how to block bad words using javascript.You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

Subscribe Our Newsletter And Get Tutorials
And Offers Via Email

Subscribe Our Newsletter And Get Tutorials And Offers Via Email

About Me


Hello, My Name is Mudit Jain. I am a Web Developer, Professional Blogger and Digital Marketer from India. I am the founder of TalkersCode. I started this blog in february 2015 to help web developers & bloggers by providing easy and best tutorials, articles and offers for web developers and bloggers.