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.
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;
}
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()
if(textbox_val=="")
bwords=badwords(textbox_val);
if(bwords>0)
{
alert("Your message contains inappropriate words.
Please clean up your message.");
document.form.textbox.focus();
<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">
onsubmit="return Message();" name="form">
<textarea name="textbox"></textarea>
<input type="submit" value=" Send "/>
</form>
Feel free post a comment.
@Phong
ReplyDeleteYa Sure ..
function badwords(txt)
ReplyDelete{
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.
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...
ReplyDelete