Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

JavaScript: Word Filter

Posted on 2007-04-05
4
Medium Priority
?
950 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 500 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

610 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