Solved

JavaScript Form Filter

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Hide cell in a table 2 27
innerHTML 7 34
100% tall div not scrollable on iPhone 3 20
asp.net open new page without popup blocker 8 18
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…

839 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