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}{font-family:verdana,arial,helvetica; font-size:8pt; color:#FFFFFF}{font-family:verdana,arial,helvetica; font-size:8pt; color:#CCCCFF} 
<script language="JavaScript"> 
// Word Filter 2.0 
// (c) 2002 Premshree Pillai 
// Created : 29 September 2002 

var swear_words_arr=new Array("hi","hello","cheers"); 
var swear_alert_arr=new Array(); 
var swear_alert_count=0; 

function reset_alert_count() 

function wordFilter(form,fields) 
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++) 
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();'); 
alert("The form cannot be submitted.nThe following illegal words were found:n_______________________________n" + alert_text + "n_______________________________"); 
return false; 
return true; 

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--> 
<form name="form1" method="get" action="" onSubmit="return wordFilter('form1',['name','email','subject','message']);"> 
<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> 
<input type="submit" value="Submit Form" class="btn"> 
<!--END FORM--> 

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. 
This example does not have swear words in it, but uses the following as the filtered words:

Times Viewed: 29

Leave a Reply

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