Word filter by lengh of the word??

Discussion in 'JavaScript' started by macaela, Jun 7, 2010.

  1. #1
    i have a word filter script but the problem is that it can be overpassed easy what i want is to count exacly the letter of the word cz for example the word cheer is banned but not the word cheerleader but the filter validates cheerleader as if it was cheer how can i get to count the words exacly
    
    <HTML>
    
    <HEAD>
    
    <TITLE>Test Input </TITLE>
    
    
    
    
    
    <!--BEGIN WORD FILTER JAVASCRIPT-->
    
    <script language="JavaScript1.2">
    
    
    
    
    
    var special_words_arr=new Array("cheer");
    
    var special_alert_arr=new Array;
    
    var special_alert_count=0;
    
    
    
    function reset_alert_count()
    
    {
    
    special_alert_count=0;
    
    }
    
    
    
    function validate_user_text()
    
    {
    
    reset_alert_count();
    
    var compare_text=document.form1.user_text.value;
    
    for(var i=0; i<special_words_arr.length; i++)
    
    {
    
      for(var j=0; j<(compare_text.length); j++)
    
      {
    
       if(special_words_arr[i]==compare_text.substring(j,(j+special_words_arr[i].length)).toLowerCase())
    
       {
    
        special_alert_arr[special_alert_count]=compare_text.substring(j,(j+special_words_arr[i].length));
    
        special_alert_count++;
    
       }
    
      }
    
    }
    
    var alert_text="";
    
    for(var k=1; k<=special_alert_count; k++)
    
    {
    
      alert_text+="\n" + "(" + k + ")  " + special_alert_arr[k-1];
    
    }
    
    if(special_alert_count>0)
    
    {
    
      alert(" these words are not valide cheer,bloody,war,terror");
    
      document.form1.user_text.select();
    
    }
    
    else
    
    {
    
       alert("well done no special words used");
    
    }
    
    }
    
    
    
    function select_area()
    
    {
    
    document.form1.user_text.select();
    
    }
    
    
    
    window.onload=reset_alert_count;
    
    
    
    </script>
    
    <!--END WORD FILTER JAVASCRIPT-->
    
    
    
    </HEAD>
    
    <BODY>
    
    
    
    <form name="form1" method="post" action="your_post_address.asp">
    
    <center><font face="Times New Roman" size="6pt" color="#606060"><b><i>Word Filter</i></b></font></center>
    
    <table><tr><td></td></tr></table>
    
    <textarea rows="3" cols="40" name="user_text" style="border:2 solid #808080; font-family:verdana,arial,helvetica; font-weight:normal; font-size:10pt" onclick="select_area()">Enter your text here...</textarea>
    
    <table><tr><td></td></tr></table>
    
    <center><input type="button" style="background:#EFEFEF; border:2 solid #808080; width:100%; cursor:pointer" value="Submit" onclick="validate_user_text();"></center>
    
    </form></form>
    
    
    
    </BODY>
    
    </HTML>
    Code (markup):

     
    macaela, Jun 7, 2010 IP
  2. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #2
    you could just change
    
    var special_words_arr=new Array("cheer");
    
    Code (markup):
    to
    
    var special_words_arr=new Array(" cheer ");
    
    Code (markup):
    Not sure if it will work for you but give it a try
     
    stephan2307, Jun 7, 2010 IP
  3. macaela

    macaela Active Member

    Messages:
    181
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    It only alert if theres space between the word cheer like " cheer " which can trick the user if the user types " i love cheer alot "

    but if it just typed
    cheer
    doesnt alert which meant to alert
     
    macaela, Jun 7, 2010 IP
  4. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #4
    Then what you need to do is to split the entire text into an array by using split() and then loop through every word in the array and see if it contains a bad word.
     
    stephan2307, Jun 8, 2010 IP