Solved

JavaScript Form Filter

Posted on 2007-04-03
3
208 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
  • 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

708 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

18 Experts available now in Live!

Get 1:1 Help Now