Loading Searchbox
9lessons programming blog logo
Monday, August 17, 2009

Inappropriate words Validation with Javascript.

4 comments
This post about how to block Inappropriate/bad words content with java script validation. Using this script we can stop bad messages/commets updates. It's very simple script take a look live demo and try to submit some bad words.

Inappropriate words Validation with Javascript.

Download Script     Live Demo


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");
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 inappropriate 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>

Feel free post a comment.

Related Posts :

Perfect Javascript Validation

Sponsored Links

Recent Posts

Share this post

Subscribe to my feeds

Subscribe
Comments
4 comments
Phong said...

very cool & good script, thank you very much for sharing.

Can you submit this code to my JavaScript library?


Awaiting your response. Thank

Srinivas Tamada said...

@Phong

Ya Sure ..

Anonymous said...

function badwords(txt)
{
    txt = txt.toLowerCase();
    var alert_count = 0;
    for(var i = 0, len = bad_words_array.length; i < len; i++)
    {
        if(txt.indexOf(bad_words_array[i]) !== -1)
        {
            alert_count++;
        }
    }
    return alert_count;
}

Simpler and more efficient.

Anonymous said...

Extremely inefficient and basically a bad idea anyway (as it will find bad words in plain words, for example). See http://www.codinghorror.com/blog/archives/001176.html for example for more details. Not to mention it is super-easy to workaround...

Post a Comment

Orkut | FacebookAbout Me

Subscribe now!Feeds RSS

Subscribe now!Recent Posts

Subscribe now!Categories

Subscribe now!Comments

People Says

@9lessons thank you for the great tutorials, we truly appreciate your contributions to the design community.

Smashing Magazine

Join into my community

Labs ProfileRelease

My ProfileTwitter