Inappropriate words Validation with Javascript

Inappropriate-words

This post about how to block Inappropriate/bad words content with Javascript validation. Using this script we can stop bad messages/commets updates.

Javascript

The tutorial contains two files.

— index.html

— badwords.js

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

var bad_words_array=new Array("badword-1","badword-2","badword-3","badword-4");
function badwords(txt)
{
var alert_arr=new Array;
var alert_count=0;
var compare_text=txt;

for(var i=0; i<bad_words_array.length; i++)
{
for(var j=0; j<(compare_text.length); j++)
{
if(bad_words_array[i]==compare_text. 
substring(j,(j+bad_words_array[i].length)).toLowerCase())
{
alert_count++;
}
}
}
return alert_count;
}

index.html
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;
if(textbox_val=="")
{
alert("Please enter a message");
return false;
}

bwords=badwords(textbox_val);
if(bwords>0)
{
alert("Your message contains bad words. Please clean up your message.");
document.form.textbox.focus();
return false;
}
}
</script>

<form action="thankyou.html" method="post"
onsubmit="return Message();" name="form">
<textarea name="textbox"></textarea>
<input type="submit" value=" Send "/>
</form>

 

If you enjoyed this tutorial and learned something from it, please consider sharing it with our friends and followers! Also like to my facebook page to get more awesome tutorial each week!

By Rodney

I’m Rodney D Clary, a web developer. If you want to start a project and do a quick launch, I am available for freelance work. info@quickmysupport.com

Leave a Reply

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