Word Filter Demo : Word Filter « Page Components « JavaScript DHTML






Word Filter Demo

<html>
<head>
<title>Word Filter</title>

<!--BEGIN WORD FILTER JAVASCRIPT-->
<script language="JavaScript1.2">

// Word Filter
// (c) 2002 Premshree Pillai
// Created : 23 June 2002
// http://www.qiksearch.com
// http://javascript.qik.cjb.net
// E-mail : qiksearch@rediffmail.com

var swear_words_arr=new Array("fuck","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>
<font face="verdana,arial,helvetica" size="-1" color="#000000">
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 <font face="courier">swear_words_arr</font>. When the user types the text and hits the submit button, if the text contains any word that is present in the array <font face="courier">swear_words_arr</font>, the form will not be submitted.
<br><br>The script can be used for validation of swear words etc.
<br><br><font size="-2" color="#808080">&#169 2002 Premshree Pillai.</font>

</font>
</td></tr></table>
<!--END FORM-->

</body>
</html>

           
       








Related examples in the same category

1.Word Filter 2.0