Last updated on February 24th, 2022 at 02:44 pm

Hi today I will explain you how to filter out inappropriate / bad words from being updated or send from your website.This is simple javascript.

Contains javascript code. If you want add some more words in bad_words_array

var bad_words_array=new Array("dog","badword-2","badword-3");
function badwords(txt)
var alert_arr=new Array;
var alert_count=0;
var compare_text=txt;
var total = bad_words_array.length;
var matches = 0;
for( var i = 0; i < total; i++ )
if(compare_text.indexOf(bad_words_array[i]) > -1)
var message=1;
return message;

Contains javascript and HTML code. The form calling Message() function.

<script type=">text/javascript" src="badwords.js"></script>
<script type=">text/javascript">
function Message()
var textbox_val=document.form.textbox.value;
alert("Please enter a message");
return false;
alert("Your message contains some bad words please remove it.");
return false;
<form action="send.php" method="post"onsubmit="return Message();" name="form">
<textarea name="textbox"></textarea>
<input type="submit" value=" Submit "/>

I have fixed couple of bugs in this script. This is working like a charm now.

2 thoughts on “How to use Javascript to filter out bad words”

Leave a Reply

Your email address will not be published. Required fields are marked *