...

Word Filter II

premshree
09-29-2002, 01:51 PM
Filter out words from multiple fields :


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


:thumbsup:

mordred
10-04-2002, 09:55 PM
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... :)

beetle
10-09-2002, 04:32 PM
Originally posted by mordred
Three suggestionsI 2nd that. Regular Expressions are your friend!

whammy
10-17-2002, 12:53 AM
See signature... ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum