Solved

JavaScript: Word Filter

Posted on 2007-04-05
4
931 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
Comment Utility
Do the same but with getElementsByTagName('TEXTAREA');

?
0
 

Author Comment

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

Expert Comment

by:jawahar_prasad
Comment Utility
<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
Comment Utility
<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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now