Solved

JavaScript: Word Filter

Posted on 2007-04-05
4
934 Views
Last Modified: 2008-02-20
OK, here is my code. I need this to check the array against a textarea as well. how can this be done?

function check() {
var bad = new Array('word1','word2','word3');
// INPUT BOXES
      var text=document.getElementsByTagName('INPUT');
      for (var i=0;i<text.length;i++)   {
          if ((text[i].type=='text')&&(text[i].value!=''))
              for (var j=0; j<bad.length;j++)   {
                   if (text[i].value.toLowerCase()==bad[j].toLowerCase())   {
                       alert('\"'+bad[j]+'\"'+'  not allowed!');
                       return false;
                  }
              }
      }
       return true;
}
0
Comment
Question by:Nitestarz
  • 2
4 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 18857606
Do the same but with getElementsByTagName('TEXTAREA');

?
0
 

Author Comment

by:Nitestarz
ID: 18857663
It doesnt seem to want to work, it ends up breaking the input box
0
 
LVL 8

Expert Comment

by:jawahar_prasad
ID: 18857712
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script language="javascript">
function check() {
var bad = new Array('word1','word2','word3');
// INPUT BOXES
      var text=document.getElementsByTagName('INPUT');
      for (var i=0;i<text.length;i++)   {
          if ((text[i].type=='text')&&(text[i].value!=''))
              for (var j=0; j<bad.length;j++)   {
                   if (text[i].value.toLowerCase()==bad[j].toLowerCase())   {
                       alert('\"'+bad[j]+'\"'+'  not allowed!');
                       return false;
                  }
              }
      }
        
        var text=document.getElementsByTagName('TEXTAREA');
      for (var i=0;i<text.length;i++)   {
          if (text[i].innerHTML!='')
              for (var j=0; j<bad.length;j++)   {
                   if (text[i].innerHTML.toLowerCase()==bad[j].toLowerCase())   {
                       alert('\"'+bad[j]+'\"'+'  not allowed!');
                       return false;
                  }
              }
      }
        
       return true;
}
</script>
</head>

<body>
<form name="form1" id="form1" method="post" action="">
  <input type="text" name="textfield" />
  <textarea name="textfield">word1</textarea>
  <br /><bR />
    <a href="#" onclick="javascript: check();">Check</a>
</form>
</body>
</html>
0
 
LVL 35

Accepted Solution

by:
TimYates earned 125 total points
ID: 18857736
<html>
<head></head>
<body>
  <script type="text/javascript">
    function check() {
      var bad = new Array('word1','word2','word3');
      // INPUT BOXES
      var text = document.getElementsByTagName('INPUT') ;
      for( var i = 0 ; i < text.length ; i++ )
      {
        if( text[i].type == 'text' )
        {
          for( var j = 0 ; j < bad.length ; j++ )
          {
            if( text[i].value.toLowerCase() == bad[j].toLowerCase() )
            {
              alert( '\"'+bad[j]+'\"'+'  not allowed!' ) ;
              return false ;
            }
          }
        }
      }
      text = document.getElementsByTagName('TEXTAREA') ;
      for( var i = 0 ; i < text.length ; i++ )
      {
        for( var j = 0 ; j < bad.length ; j++ )
        {
          if( text[i].value.toLowerCase() == bad[j].toLowerCase() )
          {
            alert( '\"'+bad[j]+'\"'+'  not allowed!' ) ;
            return false ;
          }
        }
      }
      return true ;
    }
</script>
  <input type="text" name="a"/>
  <input type="text" name="b"/>
  <textarea name="c"></textarea>
  <input type="button" onclick="check()"/>
</body>
</html>

does what you said you wanted, but I don't think it *is* what you want, as it only checks the contents of the input and textarea against a single word...

Did you want to check every word against the list of bad words?

Did you realise that people will get round this?  (using stars, spaces, underscores, whatever)

Tim
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Javasctipt 2 24
HTML5 Get data in table rows 5 26
Client Validating 2 date fields, required & comparison 1 22
Select case on click 3 14
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question