# Javascript Problems - Validation Code



## KugarWeb (Apr 6, 2007)

Hi there,

Well after a short period of a client's form being spammed, I decided to look into a way of blocking certain words from being entered into the comments box using a simple validation, ie. checking for a word and then refusing to submit the information forward, thus stopping the computer program in it's tracks.

It's incredibly simple and does not even use a captcha system, mostly because the client felt that enquiries would not be sent due to people having to "bother" with entering the required code.

So, I simply dropped in an if statement which checked for certain words which seemed to be appearing within the enquiry emails, although I seem to have stopped my code from working entirely and I'm not quite sure how, mainly because this site was built a year or so back and I haven't used the validation code since.

The "offending" code is shown below, with the entire javascript following it.


```
if(frm.elements[comments].value == 'nude' || frm.elements[comments].value == 'viagra') {
    isValid = false;
    strErrMsg += "Your comments are not appropriate\n";
  }
```


```
function validate(frm,tel,email,comments,nonBlank) {
  var isValid = true;
  var someBlank = false;
  var strErrMsg = "The following errors occurred with your submission :\t\n\n";
  var alphaChars = "abcdefghijklmnopqrstuvwxyz";

  alphaArray = new Array();
  for (var i=0; i<alphaChars.length; i++) {
    alphaArray[i] = alphaChars.charAt(i);
  }
	
	if (tel != '')
	{
		if(frm.elements[tel].value != '') {
		 for (var i=0; i<alphaArray.length; i++) {
		  if(String(frm.elements[tel].value).indexOf(alphaArray[i]) != -1) {
		   isValid = false;
		  }
		 }
		 if (isValid == false) {
		   strErrMsg += "The telephone number you entered is not a number\n";
		 }
		}
	}
	
	if (email != '')
	{
		if(frm.elements[email].value != '') {
		 if(frm.elements[email].value.indexOf('@') == -1 || frm.elements[email].value.indexOf('.') == -1 || frm.elements[email].value.indexOf(' ') != -1) {
		   isValid = false;
		   strErrMsg += "You have not supplied a valid email address\n";
		 }
		}
	}
	if(frm.elements[comments].value == 'nude' || frm.elements[comments].value == 'viagra') {
    isValid = false;
    strErrMsg += "Your comments are not appropriate\n";
  }
  var nonBlankElements = nonBlank.split(",");
  for(var x=0;x<nonBlankElements.length;x++) {
    if(frm.elements[nonBlankElements[x]].value == '') {
      someBlank = true;
    }
  }

  if(someBlank == true) {
    isValid = false;
    strErrMsg += "You have not filled in all the required (*) fields";
  }

  if(isValid == false) {
    alert(strErrMsg);
  }

  return isValid;
}
```
If someone could reply back to me as soon as possible I would be most grateful as the website is currently live.

Thank you in advance! 
Andrew.


----------



## tomdkat (May 6, 2006)

So, you're saying if you remove the if block that checks for "nude" and "viagra", the validation script works fine?

Peace...


----------



## KugarWeb (Apr 6, 2007)

tomdkat said:


> So, you're saying if you remove the if block that checks for "nude" and "viagra", the validation script works fine?
> 
> Peace...


Yeah, exactly that  I'm expecting it to be something like a missing bracket or whatever...


----------



## tomdkat (May 6, 2006)

The code looks fine. Do you have Firefox installed? If so, use that to test the JavaScript code. Load the form in Firefox and open the Error Console (Tools/Error Console). Clear any errors that might be in the window. Then fill out the form and submit it to invoke the validation. Check the Error Console for any errors.

Peace...


----------



## tomdkat (May 6, 2006)

Also, you might want to add a check of 'comments' to make sure it's not blank before you check it for the bad words.

Peace...


----------



## KugarWeb (Apr 6, 2007)

Thanks for your responses tomdkat.
I've run it through the Error Console via Firefox and it's coming back with an error "Error: frm.elements[comments] has no properties" and I'm not sure what this is referring to....


----------



## tomdkat (May 6, 2006)

That means you will need to get the "comments" element in some other method since the method you're using now isn't returning what you think it's returning.

Could you post a link to the form or post the HTML here?

Peace...


----------



## KugarWeb (Apr 6, 2007)

Yeah sure, it's www.swshadows.org/contact.asp


----------



## tomdkat (May 6, 2006)

Ok, I think I know what the problem is.

Peace...


----------



## Jackiefrost9 (Jun 17, 2004)

Is it still not working? I just tried it and it works for me.

By the way, on the actual page you have the code listed twice... Unless i'm mistaken.

I'm using firefox to view the page and firebug to look at the code.


----------



## tomdkat (May 6, 2006)

Jackiefrost9 said:


> Is it still not working? I just tried it and it works for me.


He found the problem and fixed it. 

Peace...


----------



## Jackiefrost9 (Jun 17, 2004)

Ah ok. I saw where you thought you knew what it was, but I never actually saw any solution so I just wanted to make sure.


----------



## tomdkat (May 6, 2006)

Yeah, I was waiting for him to ask what the solution was. 

Basically, on the "form" tag, he wasn't passing the "comments" form element to the validation script. The form now does that so things appear to be working. 

Peace...


----------

