Word filter by length of the word??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lisa007
    New Member
    • Apr 2010
    • 29

    Word filter by length of the word??

    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


    Code:
    <HTML>
    
    <HEAD>
    
    <TITLE>Test Input </TITLE>
    
    
    
    
    
    <!--BEGIN WORD FILTER JAVASCRIPT-->
    
    <script language="JavaScript1.2">
    
    
    
    
    
    var special_words_arr=new Array("cheer","bloody","war","terror");
    
    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>
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    this could be simplyfied with the following for example:

    Code:
    var specialWords = {
        "cheer": 1, 
        "bloody": 1, 
        "war": 1, 
        "terror": 1
    };
    
    var s = 'foo foo bloody-mary terror\nmy test text';
    
    function checkText(s) {
        var check = true;
    
        var wordList = s.split(/[ |\n]/);
    
        var forbiddenWordsInText = [];
    
        for (var i = 0, l = wordList.length; i < l; ++i) {
            var word = wordList[i];
    
            if (word in specialWords) {
                forbiddenWordsInText.push(word);
            }
        }
    
        if (forbiddenWordsInText.length > 0) {
            check = false;
            alert(forbiddenWordsInText);
        }
    
        return check;
    }
    
    checkText(s);
    it uses a JavaScript object for the special words so that an easy look up is possible and therefor it just needs one loop through the text which was splitted according to whitespaces and linebreaks ... you might simply test it in the firebug console ...

    kind regards

    PS: those check is clientside a bit useless when JavaScript is turned of or the user just modifies the script which easily could be done ... so don't only rely on this and do a serverside check when this would be a critical requirement.

    Comment

    • lisa007
      New Member
      • Apr 2010
      • 29

      #3
      this what i had using your code but as soon you load the page it simples pop up alert box saying terror then it no longer works

      Code:
      <HTML>
      <HEAD>
      <TITLE>Test Input </TITLE>
      
      
      <!--BEGIN WORD FILTER JAVASCRIPT-->
      <script language="JavaScript1.2">
      
      
      
      
       var specialWords = {
             "cheer": 1, 
          "bloody": 1, 
           "war": 1, 
            "terror": 1
         };
         
         var s = 'foo foo bloody-mary terror\nmy test text';
         
        function checkText(s) {
          var check = true;
         
            var wordList = s.split(/[ |\n]/);
        
           var forbiddenWordsInText = [];
         
            for (var i = 0, l = wordList.length; i < l; ++i) {
                 var word = wordList[i];
        
               if (word in specialWords) {
                  forbiddenWordsInText.push(word);
                }
           }
        
          if (forbiddenWordsInText.length > 0) {
              check = false;
              alert(forbiddenWordsInText);
          }
       
         return check;
       }
        
       checkText(s);
        
      
      
      
      </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>

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        of course ... you just copied and pasted the script into your page ... you would need to drop the function call and adapt your onclick handler ...

        Comment

        Working...