I am having very much difficulty trying to use a variable that I made in a previous function to work with my new function. I have tried to make the variable global but this is not working and I have no idea why. Can anyone please help me out?
Thanks![]()
You'll have to provide some code before we can tell you what went wrong.
here is my Javascript code:
This is in my BODY Tag:Code:function checkProvince() { var myForm = document.pizzaForm; if (myForm.province.value == "Nunavut" || myForm.province.value == "Yukon") { alert("We are sorry but we do not deliver here."); } } function checkName() { var errMessage = " "; var checkk = 0; var myForm = document.pizzaForm; var rePhone = /^\(?(\d{3})\)?[\.\-\/\s]?(\d{3})[\.\-\/\s]?(\d{4})$/ ; var rePostal = /^[A-Z][0-9][A-Z][0-9][A-Z][0-9]$/; //var reEmail = /^(\w)+\@(\w).(\w)$/; var validPhone = document.pizzaForm.telephone; var validPostal = document.pizzaForm.postalCode; var pizzaAmount = document.pizzaForm.pizzaNumber.value //var validEmail = document.pizzaForm.email; var finalAmount = " "; if (myForm.firstName.value == "" || myForm.lastName.value == "") { errMessage += "Please type in your name\n"; } if (myForm.address.value == "") { errMessage += "Please type in your address\n"; } if (myForm.city.value == "") { errMessage += "Please type in your city\n"; } if (myForm.postalCode.value == "") { errMessage += "Please type in your postal code\n"; } if (myForm.province.value == "") { errMessage += "Please select a province\n"; } if (myForm.telephone.value == "") { errMessage += "Please type in a phone number\n"; } if (myForm.email.value == "") { errMessage += "Please type in your email\n"; } if (!rePhone.test(validPhone.value)) { errMessage += "Please type in a valid phone number, such as: 519-555-5555\n"; } if (!rePostal.test(validPostal.value.toUpperCase())) { errMessage += "Please type in a valid postal code, such as: N2N3M8\n"; } /*if (!reEmail.test(vaildEmail.value)) { errMessage += "Please enter a vaild email such as: example@example.com\n"; }*/ if (isNaN(myForm.pizzaNumber.value)) { errMessage += "Please enter a valid number of pizzas\n"; } else if (myForm.pizzaNumber.value == 0) { errMessage += "You must order at least one pizza\n"; } //Check to see the if the Pizza Size has been selected if (!(document.pizzaForm.pizzaSize[0].checked) && !(document.pizzaForm.pizzaSize[1].checked) && !(document.pizzaForm.pizzaSize[2].checked) && !(document.pizzaForm.pizzaSize[3].checked)) { errMessage += "You must select a pizza size\n"; } //calculate order if (!(errMessage) && (document.pizzaForm.pizzaSize[0].checked)) { finalAmount += pizzaAmount * 5; } else if (document.pizzaForm.pizzaSize[1].checked) { finalAmount += pizzaAmount * 10; } else if (document.pizzaForm.pizzaSize[2].checked) { finalAmount += pizzaAmount * 15; } else if (document.pizzaForm.pizzaSize[3].checked) { finalAmount += pizzaAmount * 20 } if (errMessage != "") { alert(errMessage); } } //check to see if more then 5 toppings are selected function checkTopping() { var boxCheck = 0; for (var i=1;i<=11;i++) { boxName = "topping" + i; var toppingAmount = boxCheck * 0.75; if (document.pizzaForm[boxName].checked) { boxCheck++; } } if (boxCheck == 0) { alert( "you must select at least one topping"); } else if (boxCheck > 5) { alert( "You can only select a maximum of 5 toppings"); return false; } alert ("Total cost for topping will be: $" + toppingAmount); } //To call all functions togther function allFunctions() { checkTopping(); checkName(); } </script>
I want to finalize the validation so that once all fields are validated i can show an alert of how much the total cost of the order will be. I want to use the variable toppingAmount that is in the checkTopping function in my checkName function so I can use it and show an alert to show the total cost of the order. I tried to place it outside of all functions to make it global but that is not working. Could you please help me?Code:<form action="pizzaForm" method="get" name="pizzaForm" id="pizzaForm" onSubmit="return allFunctions()"> <table width="475" border="0"> <tr> <td width="329"> <fieldset style="width:250px"> <legend align="top">Personal Information</legend> First Name <input name="firstName" type="text" id="firstName" /><br /> Last Name <input name="lastName" type="text" id="lastName" /><br/> Address <input name="address" type="text" id="address" /><br /> City <input name="city" type="text" id="city" /><br /> Province <select name="province"> <option value="">Please Select a Province</option> <option>Alberta</option> <option>British Columbia</option> <option>Manitoba</option> <option>New Brunswick</option> <option>Newfoundland and Labrador</option> <option>Northwest Territories</option> <option>Nova Scotia</option> <option onClick="checkProvince()">Nunavut</option> <option>Ontario</option> <option>Prince Edward Island</option> <option>Québec</option> <option>Saskatchewan</option> <option onClick="checkProvince()">Yukon</option> </select><br/> Postal Code <input name="postalCode" size="9" maxlength="6" type="text" id="postalCode" /><br/> Telephone <input name="telephone" type="text" id="telephone" /><br /> Email <input name="email" type="email" id="email" /> </fieldset> <!--Order Information Groupbox --> </td> <td width="130"> <fieldset style="width:300px"> <legend align="top">Order Information</legend> Number Of Pizza's <input name="pizzaNumber" maxlength="2" type="text" id="pizzaNumber" /><br /><br /> Size<br /> <label> <input name="pizzaSize" type="radio" value="radio" /> Small</label> <br /> <label> <input type="radio" name="pizzaSize" value="radio" /> Medium</label> <br /> <label> <input type="radio" name="pizzaSize" value="radio" /> Large</label> <br /> <label> <input type="radio" name="pizzaSize" value="radio" /> Extra Large</label><br /><br /><br /> Crust Type <select name="crustType"> <option>Hand-Tossed Pizza</option> <option>Pan Pizza</option> <option>Stuffed Crust</option> <option>Thin Crust</option> </select><br /><br /> Toppings<br /> <input name="topping1" type="checkbox" id="sausage" />Sausage <input name="topping2" type="checkbox" id="pepperoni" />Pepperoni <input name="topping3" type="checkbox" id="beef" />Beef <input name="topping4" type="checkbox" id="bacon" />Bacon<br /> <input name="topping5" type="checkbox" id="chicken" />Chicken <input name="topping6" type="checkbox" id="ham" />Ham <input name="topping7" type="checkbox" id="olives" />Olives <input name="topping8" type="checkbox" id="pepper" />Peppers<br /> <input name="topping9" type="checkbox" id="tomatoes" />Tomatoes <input name="topping10" type="checkbox" id="mushrooms" />Mushrooms <input name="topping11" type="checkbox" id="pinapple" />Pinapple<br /><br /> Extra<br /> <input name="extra" type="checkbox" id="extra" />Add small (5 wings) for an additional $2.00 </fieldset> <input name="Submit" type="button" id="submit" value="Submit" onClick="allFunctions()" /> <input name="Reset" type="reset" id="reset" value="Reset" /> </td> </tr> </table> </form>
Thanks![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks