Link to home
Start Free TrialLog in
Avatar of cokefour
cokefourFlag for United Kingdom of Great Britain and Northern Ireland

asked on

My JavaScript form isn't validating properly

Hi Experts,
I'm in the middle of an assignment and part of it involves validating a form. I used the code below to validate the form and have checked that the form elements match up with what I have in the JavaScript.
I tested it it with just the first two fields including the email and it worked as expected. When I added the remaining fields only the email address field validates.
Can anyone tell me why?
function validateDetails(myForm){
	var errors = [];
	var customerName = myForm['customerName'].value;
	var emailAddress = myForm['emailAddress'].value;
	var addressLine1 = myForm['addressLine1'].value;
	var addressLine2 = myForm['addressLine2'].value;
	var cTownCity = myForm['townCity'].value;
	var cPostCode = myForm['postalCode'].value;
	var cCardNumber = myForm['paymentDetails'].value;
	
	if(customerName.value == "")
		{errors.push("Please provide your name");}
	
	if(!validateEmail(emailAddress))
	 {errors.push("Please provide a valid email");}
	 
	if (addressLine1.value == "")
	{errors.push("Please provide the first line of your address");}
	
	if(cTownCity.value=="")
	errors.push("Please fill in the Town or City field");
	
	
	if (errors.length>0){
		var errorsDiv = document.getElementById("yourorder");
		
		errorsDiv.innerHTML = errors.join("<br/>");
		return false;
	}
}
function validateQuantity()
{
	var val = this.value;
	if(isNumber())
	{
		document.getElementById("ordererrors").innerHTML = "error";
	}
}
/*validate email addresses*/
function validateEmail(emailAddress){
var emailFormat = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if(emailAddress.match(emailFormat))
return true;
}

Open in new window

User generated image
Avatar of COBOLdinosaur
COBOLdinosaur
Flag of Canada image

addressline1 is a handle for myform.addressline1.value

So if (addressline1.value is actually if (myform.adressline1.value.value) which is probably generating an error if you look in the javascript console.


Cd&
Avatar of cokefour

ASKER

Doesn't the same apply for the other variables?
I declared emailAddress and customerName the same way and email validates but not the others and customerName comes first.
My error console is also empty
C
ASKER CERTIFIED SOLUTION
Avatar of COBOLdinosaur
COBOLdinosaur
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
javascript form the javascript fires when you click on the submit button on the right hand form
Ah! I see what you mean. Can you recommend a good piece of software or article to help me debug JavaScript step by step?
For debugging script, the Firebug addon for Firefox is a good choice.  It is also used by a lot of the WebDev experts on the site, so it makes for pretty good support.  It is also probably the number one JS debugger used by professional developers.

Cd&