Solved

JavaScript Form Filter

Posted on 2007-04-03
3
226 Views
Last Modified: 2012-05-05
I have a form that has a validator to make sure certain fields are filled in. Now I need a filter to make sure a list of certain words cannot be used, such as swear words.

I need it to check the whole form, which as many fields. How can I do this?
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
  • 3
3 Comments
 
LVL 28

Expert Comment

by:TName
ID: 18845475
This will alert for every "bad" wor it has found. In this example, bad words are bad1, bad2, xxx, yyy, zzz.
Fill the array "bad" with bad words. The example is not case sensitive:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script>
function checkIt() {

      var bad = new Array('bad1','bad2','xxx','yyy','zzz');

      var count=0;
      var fields=document.getElementsByTagName('INPUT');
      for (var i=0;i<fields.length;i++)   {
          if ((fields[i].type=='text')&&(fields[i].value!=''))
              for (var j=0; j<bad.length;j++)   {
                   if (fields[i].value.toLowerCase()==bad[j].toLowerCase())   {
                       alert(bad[j]+'  not allowed');
                       count++;
                  }
              }
      }
    if (count>0)
       return false;
    else
       return true;
}
</script>
</head>
<body>
<form>
<input type="text">
<input type="text">
<input type="text">
</form>
<input type="button" onClick="checkIt();">
</body>
</html>
0
 
LVL 28

Accepted Solution

by:
TName earned 125 total points
ID: 18845514
And if you want only one alert, regardless of how many bad words are found, change the code to something like this:
(it will only alert for the first "bad" word)


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script>
function checkIt() {

      var bad = new Array('bad1','bad2','xxx','yyy','zzz');

      var fields=document.getElementsByTagName('INPUT');
      for (var i=0;i<fields.length;i++)   {
          if ((fields[i].type=='text')&&(fields[i].value!=''))
              for (var j=0; j<bad.length;j++)   {
                   if (fields[i].value.toLowerCase()==bad[j].toLowerCase())   {
                       alert('\"'+bad[j]+'\"'+'  not allowed!');
                       return false;
                  }
              }
      }
       return true;
}
</script>
</head>
<body>
<form>
<input type="text">
<input type="text">
<input type="text">
</form>
<input type="button" onClick="checkIt();">
</body>
</html>
0
 
LVL 28

Expert Comment

by:TName
ID: 18845560
If you want to check only the fields on the form, not in the entire document, you can change
   var fields=document.getElementsByTagName('INPUT');
to
   var fields=document.form1.getElementsByTagName('INPUT');
where "form1" would be your form's name.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script>
function checkIt() {

      var bad = new Array('bad1','bad2','xxx','yyy','zzz');

      var fields=document.form1.getElementsByTagName('INPUT');
      for (var i=0;i<fields.length;i++)   {
          if ((fields[i].type=='text')&&(fields[i].value!=''))
              for (var j=0; j<bad.length;j++)   {
                   if (fields[i].value.toLowerCase()==bad[j].toLowerCase())   {
                       alert('\"'+bad[j]+'\"'+'  not allowed!');
                       return false;
                  }
              }
      }
       return true;
}
</script>
</head>
<body>
<form name="form1">
<input type="text">
<input type="text">
<input type="text">
</form>
<input type="button" onClick="checkIt();">
</body>
</html>
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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

726 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