Solved

JavaScript: Word Filter

Posted on 2007-04-05
4
938 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

734 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