January 14, 2024 5:01 am

How to block Inappropriate content with javascript validation

If you are making a system where peoples come and give there reviews or accept content from public post, there is always some bad peoples write bad words and you have to manually manage these posts. Now with this article you will be able to manage inappropriate words and restrict peoples to write on website. Its a simple and easy to implement program hope you enjoy it.

bad-words

[wpdm_file id=27]DEMO

create Bad word validation first create inappropriate.js File.

var words_array=new Array("bad","hate","wrong");
function badwords(txt)
{
 var arr=new Array;
 var count=0;
 var text=txt;
 for(var i=0; i<words_array.length; i++)
 {
  for(var j=0; j<(text.length); j++)
  {
   if(words_array[i]==text.substring(j,(j+words_array[i].length)).toLowerCase())
   {
    count++;
   }
  }
 }
 return count;
}

Javascript and HTML code.

<script type="text/javascript" src="inappropriate.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>

I love to respond your comments.

Author Huzoor Bux

I am Huzoor Bux from Karachi (Pakistan). I have been working as a PHP Developer from last 5+ years, and its my passion to learn new things and implement them as a practice. Basically I am a PHP developer but now days exploring more in HTML5, CSS and jQuery libraries.


Tutorial Categories:

One response to “How to block Inappropriate content with javascript validation”

  1. Superlobster says:

    Good article. TBH usually the only thing I care to filter out is hyperlinks to spammy websites and the “we can boost your google ranking” guys.

Leave a Reply

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