Live Chat & Podcast at 1:00PM Eastern on Sunday!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Web Design & Development
Tag Cloud
access acer asus bios bsod computer crash desktop driver drivers error ethernet excel freeze gaming graphics hard drive hardware hdmi internet laptop malware memory monitor motherboard network operating system printer problem ram registry router slow software sound svchost.exe trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Internet & Networking > Web Design & Development >
Javascript Problems - Validation Code

Reply  
Thread Tools
KugarWeb's Avatar
Computer Specs
Member with 138 posts.
 
Join Date: Apr 2007
Experience: Intermediate
07-May-2008, 05:11 PM #1
Javascript Problems - Validation Code
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.

Code:
if(frm.elements[comments].value == 'nude' || frm.elements[comments].value == 'viagra') {
    isValid = false;
    strErrMsg += "Your comments are not appropriate\n";
  }
Code:
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's Avatar
Computer Specs
Distinguished Member with 7,127 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
07-May-2008, 05:15 PM #2
So, you're saying if you remove the if block that checks for "nude" and "viagra", the validation script works fine?

Peace...
KugarWeb's Avatar
Computer Specs
Member with 138 posts.
 
Join Date: Apr 2007
Experience: Intermediate
07-May-2008, 05:18 PM #3
Quote:
Originally Posted by tomdkat View Post
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's Avatar
Computer Specs
Distinguished Member with 7,127 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
07-May-2008, 05:59 PM #4
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's Avatar
Computer Specs
Distinguished Member with 7,127 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
07-May-2008, 06:02 PM #5
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's Avatar
Computer Specs
Member with 138 posts.
 
Join Date: Apr 2007
Experience: Intermediate
07-May-2008, 06:21 PM #6
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's Avatar
Computer Specs
Distinguished Member with 7,127 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
07-May-2008, 06:53 PM #7
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's Avatar
Computer Specs
Member with 138 posts.
 
Join Date: Apr 2007
Experience: Intermediate
07-May-2008, 06:56 PM #8
tomdkat's Avatar
Computer Specs
Distinguished Member with 7,127 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
07-May-2008, 07:01 PM #9
Ok, I think I know what the problem is.

Peace...
Jackiefrost9's Avatar
Computer Specs
Senior Member with 3,799 posts.
 
Join Date: Jun 2004
Location: Spokane, Wa
Experience: Advanced
09-May-2008, 05:35 AM #10
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's Avatar
Computer Specs
Distinguished Member with 7,127 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
09-May-2008, 11:28 AM #11
Quote:
Originally Posted by Jackiefrost9 View Post
Is it still not working? I just tried it and it works for me.
He found the problem and fixed it.

Peace...
Jackiefrost9's Avatar
Computer Specs
Senior Member with 3,799 posts.
 
Join Date: Jun 2004
Location: Spokane, Wa
Experience: Advanced
09-May-2008, 04:00 PM #12
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's Avatar
Computer Specs
Distinguished Member with 7,127 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
09-May-2008, 04:53 PM #13
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...
Reply

Tags
form validation, javascript

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 01:24 AM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.