All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Block Bad Words Using JavaScript Validation

Last Updated : Jul 1, 2023

IN - HTML JavaScript | Written & Updated By - Dikshita

In this tutorial we will show you how to block bad words using JavaScript Validation, 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.

You may also like block user from website using PHP.

Block Bad Words Using JavaScript Validation

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

<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];
  document.getElementById("bad_notice").innerHTML="Some Bad Words In Your Text!";
<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">

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.

I hope this tutorial on block bad words using javascript helps you and the steps and method mentioned above are easy to follow and implement.