JavaScript DHTML/Page Components/Word Filter

Материал из Web эксперт
Перейти к: навигация, поиск

Word Filter 2.0

   <source lang="html4strict">

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>Word Filter 2.0</title> <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>

<script language="JavaScript"> // Word Filter 2.0 // (c) 2002 Premshree Pillai // Created : 29 September 2002 // http://www.qiksearch.ru // http://javascript.qik.cjb.net // E-mail : qiksearch@rediffmail.ru var swear_words_arr=new Array("fuck","bloody","war","terror","ass"); 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>

</head> <body bgcolor="#FFFFFF">

Word Filter 2.0
 
 <form name="form1" method="get" action="" onSubmit="return   wordFilter("form1",["name","email","subject","message"]);">
Name :<input type="text" name="name" class="ip">
E-mail :<input type="text" name="email" class="ip">
Subject :<input type="text" name="subject" class="ip">
Message<textarea name="message" rows="5" cols="30"></textarea>
 <input type="submit" value="Submit Form" class="btn">
 
 </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.
 

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.

Place the <script> in the <HEAD> section of your page.

In this version, you can validate multiple form fields. View the source and see the section marked <--BEGIN FORM-->

The script can be used for validation of swear words etc. </td></tr></table>
© 2002 Premshree Pillai. </td></tr></table>

</td></tr></table>

</body> </html>

      </source>
   
  


Word Filter Demo

   <source lang="html4strict">

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

<script language="JavaScript1.2"> // Word Filter // (c) 2002 Premshree Pillai // Created : 23 June 2002 // http://www.qiksearch.ru // http://javascript.qik.cjb.net // E-mail : qiksearch@rediffmail.ru 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>

</head> <body bgcolor="#FFFFFF">

<form name="form1" method="post" action="post.cgi">

Word Filter

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

<input type="button" style="background:#EFEFEF; border:2 solid #808080; width:100%; cursor:pointer" value="Submit" onclick="validate_user_text();">

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

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.

The script can be used for validation of swear words etc.

&#169 2002 Premshree Pillai.

</body> </html>

      </source>