Solved

JavaScript Form Filter

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

695 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