Bad Word Filter

[Total: 0    Average: 0/5]

This JavaScript is a “Word Filter”. It is a type of form validator.

Head Code:

<style type="text/css"> 
..ip{border:#000000 solid 1px; width:80%} 
..btn{border:#000000 solid 1px; background:#EFEFEF; font-weight:bold; cursor:hand} 
..link{font-family:verdana,arial,helvetica; font-size:8pt; color:#FFFFFF} 
..link:hover{font-family:verdana,arial,helvetica; font-size:8pt; color:#CCCCFF} 
</style> 
<!--BEGIN WORD FILTER JAVASCRIPT--> 
<script language="JavaScript"> 
// Word Filter 2.0 
// (c) 2002 Premshree Pillai 
// Created : 29 September 2002 
// http://www.qiksearch.com 
// http://javascript.qik.cjb.net 

var swear_words_arr=new Array("hi","hello","cheers"); 
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]] + '.focus();'); 
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> 

Body Code:

<table cellspacing="0" cellpadding="0" bgcolor="#0066CC" align="center" width="446"><tr><td></td></tr></table> 
<table cellspacing="0" cellpadding="0" bgcolor="#0066CC" align="center" width="448"><tr><td></td></tr></table> 
<table style="border:#0066CC solid 2px; width:450px" align="center" cellspacing="0" cellpadding="0"><tr><td> 
<table style="width:100%; background:#0066CC"><tr><td align="middle"><span style="font-family:courier new,courier,verdana,arial; font-weight:bold; font-size:30pt; color:#FFFFFF; filter:Shadow(direction=45, color=#000000); width:100%">Word Filter 2.0</span></td></tr></table> 
<!--BEGIN FORM--> 
<table><tr><td> 
<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" class="ip" value="hi"></td></tr> 
<tr><td>E-mail :</td><td><input type="text" name="email" class="ip" value="hello"></td></tr> 
<tr><td>Subject :</td><td><input type="text" name="subject" class="ip"></td></tr> 
<tr><td>Message</td><td><textarea name="message" rows="5" cols="30">cheers</textarea></td></tr> 
</table> 
<input type="submit" value="Submit Form" class="btn"> 
</form> 
</td></tr></table> 
<!--END FORM--> 

<table><tr><td> 
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. 
<br><br>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. 
<br><br>Place the &lt;script&gt; in the &lt;HEAD&gt; section of your page. 
<br><br><b>In this version, you can validate multiple form fields. View the source and see the section 
marked </b>&lt;--BEGIN FORM--&gt; 
<br><br>The script can be used for validation of swear words etc. 
</td></tr></table> 
<BR>
<BR>
This example does not have swear words in it, but uses the following as the filtered words:
<list><li>"hi"<li>"hello"<li>"cheers"</list>

Times Viewed: 11

Leave a Reply

Your email address will not be published. Required fields are marked *