Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-24-2011, 04:36 PM   PM User | #1
cameldan
New to the CF scene

 
Join Date: Jan 2010
Location: Southampton, UK
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
cameldan is an unknown quantity at this point
Newby Form Validation

Hi,
Using Dreamweaver cs4 on a Win7 pc.
I am aware that form validation has probably been done to death and that the answer to my question is probably blatantly obvious, but after several hours of experimenting and searching, I've finally given up! So here goes...
I am experimenting with form validation. I have some basic code that works, but when I try to put it into any sort of function, it stops working, eg
here is what works:
Code:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
<TITLE>test form1</TITLE>

<SCRIPT LANGUAGE="JavaScript">
function validate() {
	if (document.Subscribe.Name.value.length < 1) {
	alert("Please enter a value for name");
	setTimeout(function() {document.Subscribe.Name.focus(); }, 10);
	return false;
	}
       if (document.Subscribe.Email.value.length < 1) {
       alert("Please enter an Email Address.");
       return false;
       } 
    return true;
    }
</SCRIPT>

</HEAD>
 
<BODY>

    <FORM name="Subscribe" Action="resultspage.asp" onSubmit="return validate();">
    name:<input type="text" name="Name"><br>
    email:<input type="text" name="Email"><br>    <input type=submit value="Submit">
    </FORM>

</BODY>
</HTML>
If I change the code to add my isDEmpty function and call it from my validate function, then everything stops working.
Code:
function isDEmpty(elName, niceName){
	if (elName.value.length < 1) {
	alert("Please enter a value for " + niceName);
	setTimeout(function() {elName.focus(); }, 10);
	return true;
	}
}

function validate() {
    if isDEmpty(document.Subscribe.Name, "Customerrrr Name") {
		return false;
	}
Can anybody explain to me what I am doing wrong? I guess a basic understanding of Javascript would help, but at the moment I am just hacking about trying to get my head around the syntax and how it all goes together... you gotta start somewhere, right?

Thanks in advance for any help.

Dan
cameldan is offline   Reply With Quote
Old 03-24-2011, 04:56 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
That will screw things up!

<script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead.

Form validation of the pattern if (document.Subscribe.Email.value.length < 1) { is barely worthy of the name, and virtually useless, as even a single space, an X or a ? will return false, that is pass the validation. Numeric values, such as zip codes and phone numbers, should be validated as such. Ditto email addresses. This topic has been covered many times before in this forum.

I don't understand what your function isDempty() is supposed to do. You have already checked for an empty string.

“The old believe everything; the middle aged suspect everything; the young know everything.” - Oscar Wilde (Irish Poet, Novelist, Dramatist and Critic, 1854-1900)
Philip M is offline   Reply With Quote
Old 03-24-2011, 05:11 PM   PM User | #3
cameldan
New to the CF scene

 
Join Date: Jan 2010
Location: Southampton, UK
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
cameldan is an unknown quantity at this point
Hi Philip, thanks for taking the time to reply.

The code that I posted is purely an experiment to try to educate my brain. I find if I can make something work then I get a better understanding, so the fact that is serves no useful purpose is kind of irrelevant at this point.

The isDEmpty function is purely an attempt to take that part of the code out of the validate function and put it into its own function so that it is callable/reusable. But it doesn't work and I don't understand why.

I have changed the script line as advised to <script type = "text/javascript">. I guess I will find some reading on this somewhere!
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>... have taken this line out but, my isDEmpty still doesn't work.

Thanks again.

Dan
cameldan is offline   Reply With Quote
Old 03-24-2011, 05:23 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Use your error console or Firebug to identify syntax errors.

if (isDEmpty(document.Subscribe.Name, "Customerrrr Name")) {

But as I say a test for just an empty string is close to useless. In practice most fields require their own unique validation function which is not very re-useable because they require different patterns. (FirstName, LastName, Address, City, Email, Phone). Beware of "all-singing-all-dancing" validation scripts.

Last edited by Philip M; 03-24-2011 at 05:29 PM..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
cameldan (03-24-2011)
Old 03-24-2011, 05:30 PM   PM User | #5
cameldan
New to the CF scene

 
Join Date: Jan 2010
Location: Southampton, UK
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
cameldan is an unknown quantity at this point
Brilliant!
I knew I wasn't far off.

Thanks for the hints about the error console (not found it yet, but will continue looking) and Firebug <scurries off to download>.
cameldan is offline   Reply With Quote
Old 03-24-2011, 05:34 PM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
In IE - Tools > Internet Options > Advanced > Browsing > Check the box - Display a notification of every script error.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 03:02 PM.


Advertisement
Log in to turn off these ads.