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 4 of 4

Thread: Word Filter II

  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 II

    Filter out words from multiple fields :

    Code:
    <html>
    <head>
    <title>Word Filter</title>
    <!--BEGIN WORD FILTER JAVASCRIPT-->
    <script language="JavaScript">
    // Word Filter 2.0
    // By Premshree Pillai
    // http://www.qiksearch.com
    
    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 wordFilter(form,fields)
    {
    	reset_alert_count();
    	var compare_text;
    	var fieldErrArr=new Array();
    	var fieldErrIndex=0;
    	for(var i=0; i<fields.length; i++)
    	{
    		eval('compare_text=document.' + form + '.' + fields[i] + '.value;');
    		for(var j=0; j<swear_words_arr.length; j++)
    		{
    			for(var k=0; k<(compare_text.length); k++)
    			{
    				if(swear_words_arr[j]==compare_text.substring(k,(k+swear_words_arr[j].length)).toLowerCase())
    				{
    					swear_alert_arr[swear_alert_count]=compare_text.substring(k,(k+swear_words_arr[j].length));
    					swear_alert_count++;
    					fieldErrArr[fieldErrIndex]=i;
    					fieldErrIndex++;
    				}
    			}
    		}
    	}
    	var alert_text="";
    	for(var k=1; k<=swear_alert_count; k++)
    	{
    		alert_text+="\n" + "(" + k + ")  " + swear_alert_arr[k-1];
    		eval('compare_text=document.' + form + '.' + fields[fieldErrArr[0]] + '.select();');
    	}
    	if(swear_alert_count>0)
    	{
    		alert("The form cannot be submitted.\nThe following illegal words were found:\n_______________________________\n" + alert_text + "\n_______________________________");
    		return false;
    	}
    	else
    	{
    		return true;
    	}
    }
    </script>
    <!--END WORD FILTER JAVASCRIPT-->
    </head>
    <body bgcolor="#FFFFFF">
    
    <!--BEGIN FORM-->
    <font face="verdana,arial,helvetica" size="-1">
    <form name="form1" method="get" action="" onSubmit="return wordFilter('form1',['name','email','subject','message']);">
    <table>
    <tr><td>Name :</td><td><input type="text" name="name"></td></tr>
    <tr><td>E-mail :</td><td><input type="text" name="email"></td></tr>
    <tr><td>Subject :</td><td><input type="text" name="subject"></td></tr>
    <tr><td>Message</td><td><textarea name="message" rows="5" cols="30"></textarea></td></tr>
    </table>
    <input type="submit" value="Submit Form">
    </font>
    </form>
    <!--END FORM-->
    
    </body>
    </html>

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Nice.

    Three suggestions:

    1. Why not use Regular Expressions?
    For such a task (pattern matching) were RegExp designed and implemented. I would have made small script that loops through an array of badwords, each time constructing a new RegExp out of the current badword and testing if it can be found within the text (and with word boundaries on either side of the word). Alternatively, create one large RegExp with a lot of alternations (|) in it.

    2. It is to rigid.
    If I would want to prevent the word "boo", your script would complain that this word is in this string: "taboo". The characters of boo are certainly there, but they are forming a different word that's not on the badword list.
    Again, RegExp would be quite helpful here.

    3. Use of eval()
    On some occasions you use eval() which I think is not necessary, you can easily look up an element's value by associative array syntax:

    eval('compare_text=document.' + form + '.' + fields[i] + '.value;');

    could be become

    compare_text = document.forms[form].elements[fields[i]].value;

    Save CPU time...

  • #3
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by mordred
    Three suggestions
    I 2nd that. Regular Expressions are your friend!
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    See signature...
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)


  •  

    Posting Permissions

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