[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 833
  • Last Modified:

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

Errors appear above the form but only the email is displayed
0
cokefour
Asked:
cokefour
  • 3
  • 3
1 Solution
 
COBOLdinosaurCommented:
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&
0
 
cokefourAuthor Commented:
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
0
 
COBOLdinosaurCommented:
They are all wrong except email address.
Email address works because you are passing and using emailAddress and not emailAddress.value

I can't really determine what else is wrong from just a piece of code.  A link to the page would help so we can see what id going on.

Cd&
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
cokefourAuthor Commented:
javascript form the javascript fires when you click on the submit button on the right hand form
0
 
cokefourAuthor Commented:
Ah! I see what you mean. Can you recommend a good piece of software or article to help me debug JavaScript step by step?
0
 
COBOLdinosaurCommented:
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&
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now