Solved

JavaScript Form Filter

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

772 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