Wrox Programmer Forums
|
BOOK: JavaScript 24-Hour Trainer
This is the forum to discuss the Wrox book JavaScript 24-Hour Trainer Jeremy McPeak; ISBN: 978-0-470-64783-7
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: JavaScript 24-Hour Trainer section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 15th, 2012, 09:56 AM
Registered User
 
Join Date: Jan 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Lesson 28

Hey Jeremy,

The problem that I am running into for this lesson is that the code doesn't seem to want to work properly (isn't that always the problem? lol). The thing is when I bring up the Web developer toolbar it isn't showing any errors and it also shows that my scripts are linking to the html page properly, so I'm a little lost at the moment. Here is my HTML & script.

HTML:
Code:
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<title>Lesson 28: Example 01</title>
	</head>
	<body>
		<form name="theForm" action="" onsubmit="validateForm(event)">
			<p>
				Name: <input type="text" name="txtName">
			</p>
			<p>
				Age: <input type="text" name="txtAge">
			</p>
			<p> 
				Email: <input type="text" name="txtEmail">
			</p>
			<p>
				Password: <input type="password" name="txtPassword1">
			</p>
			<p>
				Retype Password: <input type="password" name="txtPassword2">
			</p>

			<input name="btnSubmit" type="button" value="Submit">
		</form>

		<script src="eventUtility.js"></script>
		<script src="script.js"></script>
	</body>
</html>
and the Script:
Code:
function isEmpty(value) {
	return (value === ""); //returns a boolean value
}

function validateForm(event) {
	var theForm = document.theForm;
	var txtName = theForm.txtName;
	var txtAge = theForm.txtAge;
	var txtEmail = theForm.txtEmail;
	var txtPassword1 = theForm.txtPassword1;
	var txtPassword2 = theForm.txtPassword2;
	var button = theForm.btnSubmit;
	var age = parseInt(txtAge.value, 10);

	button.disabled = true;

	//validate name field
	if (!isEmpty(txtName.value)) {

		//validate age
		if (!isNaN(age) && age > 0) {

			//validate Email field
			if (txtEmail.value.indexOf("@") > 0) {

				//validate password - pass 1
				if (!isEmpty(txtPassword1.value)) {

					//validate password - pass 2
					if (txtPassword1.value === txtPassword2.value) {
						return;
					} else {
						alert("Passwords do not match. Please reenter them.");
						txtPassword1.focus();
						txtPassword1.select();
					}
				} else {
					alert("Password cannot be blank.");
					txtPassword1.focus();
					txtPassword1.select();
				}
				
			} else {
				alert("Please enter a valid email address.");
				txtEmail.focus();
				txtEmail.select();
			}

		} else {
			alert("Please enter your age,");
			txtAge.focus();
			txtAge.select();
		}

	} else {
		alert("Please enter your name.");
		txtName.focus();
	}

	button.disabled = false;
	eventUtility.preventDefault(event);
}
Thanks again!
 
Old October 17th, 2012, 11:13 AM
jmcpeak's Avatar
Wrox Author
 
Join Date: Nov 2005
Posts: 87
Thanks: 0
Thanked 18 Times in 17 Posts
Default

Howdy, Julsfelic.

The issue is the submit button's type attribute. It is set as "button", which by default does not submit a form. Change that to "submit", and you'll be good to go.
 
Old October 17th, 2012, 01:57 PM
Registered User
 
Join Date: Jan 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks! Debugging is still something I need to get used to, especially when there is no error thrown and it is a simple mistake like that. Thanks again!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 28 Sample Code randaljay BOOK: Professional C# 4.0 and .NET 4 3 July 31st, 2013 05:38 AM
Errors LESSON CS 28 & 29 FINAL MRGLENN BOOK: PHP and MySQL 24-Hour Trainer 2 December 7th, 2012 09:06 PM
Lesson 28 Passwords JoyH BOOK: PHP and MySQL 24-Hour Trainer 2 April 5th, 2012 12:06 PM
List2-2 at p.28 pamd4096 BOOK: Professional Android 2 Application Development 2 June 12th, 2010 10:50 PM
Ch 2 pp. 28 mdfinch BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio 7 August 24th, 2009 04:34 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.