Javascript help - validating and doing calculations in an html form?

Dead Wizard

New member
I have this script:

function valName() {
valid = true;

if (getElementById("name").value = "")
{
alert("Please enter your name");
valid = false;
}

else
{
valid = true;
}
}

function valAddress() {
valid = true;

if (getElementById("address").value = "")
{
alert("Please enter your address");
valid = false;
}

else
{
valid = true;
}
}

function valCrust() {
valid = true;
var theCrust = document.pizzaForm.crust;
var totalC = 0;

if (theCrust[0].checked)
{
valid = true;
totalC = totalC + 5;
}

if (theCrust[1].checked)
{
valid = true;
totalC = totalC + 6;
}

if (theCrust[2].checked)
{
valid = true;
totalC = totalC + 7;
}

else
{
valid = false;
totalC = totalC;
]
}

function valTopp() {
valid = true;
var theTopp = document.pizzaForm.topping;
var totalT = 0;

if (theTopp[0].checked)
{
valid = true;
totalT = totalT + 1;
}

if (theCrust[1].checked)
{
valid = true;
totalT = totalT + 0.5;
}

if (theCrust[2,3].checked)
{
valid = true;
totalT = totalT + 2;
}

else
{
valid = false;
totalT = totalT;
}
}

function addTotal()
{
var totalP = hello;
var getElementById("total").value = totalP;
}


Which is supposed to validate the data in the form fields and calculate the cost of a pizza (crust + toppings) then print the answer in the 'total' box:

<form id="pizzaForm">

Your Name <input type="text" id="name" onChange="valName()" />
<br />
Your Address <input type="text" id="address" onChange="valAddress()" />

<p />

Choose your crust:
<br />
Thin - $5 <input type="radio" name="crust" id="thin" onClick="valCrust" />
<br />
Stuffed - $6 <input type="radio" name="crust" id="stuffed" onClick="valCrust" />
<br />
Whole Wheat - $7 <input type="radio" name="crust" id="wheat" onClick="valCrust" />

<p />

Choose your toppings:
<br />
Pepperoni - $1 <input type="checkbox" name="topping" onClick="valTopp()" />
<br />
Olives - $0.50 <input type="checkbox" name="topping" onClick="valTopp()" />
<br />
Peppers -$2 <input type="checkbox" name="topping" onClick="valTopp()" />
<br />
Extra Cheese - $2 <input type="checkbox" name="topping" onClick="valTopp()" />

<p />

<input type="button" value="Total Your Order" id="totalBtn" onClick="addTotal()" />

<p />

Total: <input type="text" id="total" />

</form>

The problem is that it doesn't work at all. I am totally new to JS and have no idea what I'm doing wrong! Any help at all would be much appreciated :)
 
Back
Top