Solved

JavaScript: Word Filter

Posted on 2007-04-05
4
941 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
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…

691 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