Wednesday 20 November 2013

How to block Inappropriate words with javascript validation


This post about how to block Inappropriate/bad words content with java script validation.
Its a simple and easy to implement program hope you enjoy it.





First create inappropriate.js File for Bad word validation checking purpose.
var bad_words_array=new Array("test","wrong","waste");//type your own words.
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.php file for Javascript and HTML code.
<script type="text/javascript" src="js/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.");
                        document.form.textbox.focus();
                        return false;
                    }
                }
             </script>



Demo
KnowledgeCorner-inappropriate example
Type: "test","wrong" ,"xxx"

No comments:

Post a Comment