Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Word Filter

  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Mumbai, India
    Posts
    218
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Word Filter

    This JavaScript is a "Word Filter". It is a type of form validator. When the user submits some text, the validator will check the text for words that has to be filtered.

    The words that have to be filtered must be added to the array swear_words_arr. When the user types the text and hits the submit button, if the text contains any word that is present in the array swear_words_arr, the form will not be submitted.

    The script can be used for validation of swear words etc.

    Code:
    <html>
    <head>
    <title>Word Filter</title>
    
    <!--BEGIN WORD FILTER JAVASCRIPT-->
    <script language="JavaScript1.2">
    
    // Word Filter
    // (c) 2002 Premshree Pillai
    // http://www.qiksearch.com
    // http://javascript.qik.cjb.net
    
    var swear_words_arr=new Array("bloody","war","terror");
    var swear_alert_arr=new Array;
    var swear_alert_count=0;
    
    function reset_alert_count()
    {
     swear_alert_count=0;
    }
    
    function validate_user_text()
    {
     reset_alert_count();
     var compare_text=document.form1.user_text.value;
     for(var i=0; i<swear_words_arr.length; i++)
     {
      for(var j=0; j<(compare_text.length); j++)
      {
       if(swear_words_arr[i]==compare_text.substring(j,(j+swear_words_arr[i].length)).toLowerCase())
       {
        swear_alert_arr[swear_alert_count]=compare_text.substring(j,(j+swear_words_arr[i].length));
        swear_alert_count++;
       }
      }
     }
     var alert_text="";
     for(var k=1; k<=swear_alert_count; k++)
     {
      alert_text+="\n" + "(" + k + ")  " + swear_alert_arr[k-1];
     }
     if(swear_alert_count>0)
     {
      alert("The form cannot be submitted.\nThe following illegal words were found:\n_______________________________\n" + alert_text + "\n_______________________________");
      document.form1.user_text.select();
     }
     else
     {
      document.form1.submit();
     }
    }
    
    function select_area()
    {
     document.form1.user_text.select();
    }
    
    window.onload=reset_alert_count;
    
    </script>
    <!--BEGIN WORD FILTER JAVASCRIPT-->
    
    </head>
    <body bgcolor="#FFFFFF">
    
    <!--BEGIN FORM-->
    <table cellpadding="10" style="border:2 solid #FF9900" width="200" align="center"><tr><td>
    <form name="form1" method="post" action="post.cgi">
    <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>
    </td></tr></table>
    <!--END FORM-->
    
    </body>
    </html>

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Couldn't you just use this script:

    Code:
    var bad=["bloody","war","terror"];
    function validString(str) {
    var detected=[];
    for(var i=0;i<bad.length;i++) 
    if(str.indexOf(bad[i])!=-1)  {
    detected.unshift(bad[i]);
    break;
    }
    if(detected.length>1) alert("The following curse words were found in your entry: "+detected.join(", ");
    }
    Happy coding!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •