There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios boot browser bsod computer cpu crash css dell desktop driver dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware help please hijackthis hjt install internet internet explorer itunes javascript keyboard lan laptop log malware monitor network networking openoffice outlook outlook 2003 outlook express password popups problem router seo slow sound sp3 spyware startup trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless youtube
Web Design & Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Internet & Networking > Web Design & Development >
Javascript Problems - Validation Code


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
KugarWeb's Avatar
Computer Specs
Member with 67 posts.
 
Join Date: Apr 2007
Experience: Intermediate
07-May-2008, 04: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 2,970 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
07-May-2008, 04: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 67 posts.
 
Join Date: Apr 2007
Experience: Intermediate
07-May-2008, 04: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 2,970 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
07-May-2008, 04: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 2,970 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
07-May-2008, 05: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 67 posts.
 
Join Date: Apr 2007
Experience: Intermediate
07-May-2008, 05: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 2,970 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
07-May-2008, 05: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 67 posts.
 
Join Date: Apr 2007
Experience: Intermediate
07-May-2008, 05:56 PM #8
tomdkat's Avatar
Computer Specs
Distinguished Member with 2,970 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
07-May-2008, 06:01 PM #9
Ok, I think I know what the problem is.

Peace...
Jackiefrost9's Avatar
Computer Specs
Distinguished Member with 3,786 posts.
 
Join Date: Jun 2004
Location: Spokane, Wa
Experience: Advanced
09-May-2008, 04: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 2,970 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
09-May-2008, 10: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
Distinguished Member with 3,786 posts.
 
Join Date: Jun 2004
Location: Spokane, Wa
Experience: Advanced
09-May-2008, 03: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 2,970 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
09-May-2008, 03: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


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

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 04:39 PM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.