PDA

View Full Version : validation form project.... need help please!



JasonChan
30 Jan 2007, 05:11 AM
im fairly new at javascript but i need to write a script for a project and don't know where to begin. basically it involves form validation... i guess here's the breakdown.

I have created a form in html already and need to figure out how to write this script.

1.
select any number of products (checkboxes) and set quantities for each. Must set at least one product and selected products must have a quantity value > 0.

2. The second portion involves shipping information (ie. Name, Address, City, State, Zip, phone number, and email).
The fields must all be filled in. The state must be a valid state code (two letters). The phone number must be all digits and 10 digits. The email must be formed at least like x@y.

3. i then need to have a subtotal of the items, based on how many were selected and such.

any help would be greatly appreciated,
thanks in advance.

JasonChan
30 Jan 2007, 06:25 PM
can anyone tell me how i can set the value of a check box to 1 when clicked and a value of zero when it is unclicked?

ive done my homework and this is what i have so far:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Validation Form</title>

<script>

function checkValid()

{
if

(document.myForm.ckbox1.checked == false &&
document.myForm.ckbox2.checked == false &&
document.myForm.ckbox3.checked == false)

{
alert ("You didn't select any product");
return false;
}

else
{
return true;
}
}

</script>
</head>

<body bgcolor="#555555">

<form action="#" method="post" name="myForm" id="myForm" onsubmit="return checkValid();">
<table align="center" border="1">
<tr>
<td><input type="checkbox" id="ckbox1" name="ckbox1" onclick="document.myForm.p1.value=1;" /> Product 1</td>
<td>@ 1.25 <input type="text" id="p1" name="p1" size="5" /> Quantity</td>
</tr>
<tr>
<td><input type="checkbox" id="ckbox2" name="ckbox2" /> Product 2</td>
<td>@ 2.25 <input type="text" id="p2" name="p2" size="5" /> Quantity</td>
</tr>
<tr>
<td><input type="checkbox" id="ckbox3" name="ckbox3" /> Product3</td>
<td>@ 3.25 <input type="text" id="p3" name="p3" size="5" /> Quantity</td>

</tr>
<tr>
<td>Ship to:</td>
<td>
<table border="1">
<tr>
<td>Name:</td>
<td><input type="text" name="name" id="name" /></td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" name="address" id="address" /></td>
</tr>
<tr>
<td>City:</td>
<td><input type="text" name="city" id="city" /></td>
</tr>
<tr>
<td>State:</td>
<td>
<select>
<option value="">--Select State--</option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="FL">Florida</option>
<option value="GA">Georgia</option>
<option value="HI">Hawaii</option>
<option value="ID">Idaho</option>
<option value="IL">Illinois</option>
<option value="IN">Indiana</option>
<option value="IA">Iowa</option>
<option value="KS">Kansas</option>
<option value="KY">Kentucky</option>
<option value="LA">Louisiana</option>
<option value="ME">Maine</option>
<option value="MD">Maryland</option>
<option value="MA">Massachusetts</option>
<option value="MI">Michigan</option>
<option value="MN">Minnesota</option>
<option value="MS">Mississippi</option>
<option value="MO">Missouri</option>
<option value="MT">Montana</option>
<option value="NE">Nebraska</option>
<option value="NV">Nevada</option>
<option value="NH">New Hampshire</option>
<option value="NJ">New Jersey</option>
<option value="NM">New Mexico</option>
<option value="NY">New York</option>
<option value="NC">North Carolina</option>
<option value="ND">North Dakota</option>
<option value="OH">Ohio</option>
<option value="OK">Oklahoma</option>
<option value="OR">Oregon</option>
<option value="PA">Pennsylvania</option>
<option value="PR">Puerto Rico</option>
<option value="RI">Rhode Island</option>
<option value="SC">South Carolina</option>
<option value="SD">South Dakota</option>
<option value="TN">Tennessee</option>
<option value="TX">Texas</option>
<option value="UT">Utah</option>
<option value="VT">Vermont</option>
<option value="VA">Virginia</option>
<option value="WA">Washington</option>
<option value="WV">West Virginia</option>
<option value="WI">Wisconsin</option>
<option value="WY">Wyoming</option>
</select>
</td>
</tr>
<tr>
<td>Zip:</td>
<td><input type="text" name="zip" id="zip" /></td>
</tr>
<tr>
<td>Phone:</td>
<td><input type="text" name="phone" id="phone" /></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email" id="email" /></td>
</tr>
</table>
</td>
</tr>
<tr>
<td>Shipping:</td>
<td>
<select>
<option value="standard">Standard</option>
<option value="2-3 days">2-3 days</option>
<option value="Overnight">Overnight</option>
</select>
</td>
</tr>
<tr>
<td><input type="button" value="subtotal" name="subtotal" id="subtotal" /></td>
<td><input type="text" name="total" id="total" /></td>
</tr>
<tr>
<td><input type="radio" name="mc" id="mc" />MasterCard</td>
<td rowspan="2">
<input type="text" name="total" id="total" />
</td>
</tr>
<tr>
<td><input type="radio" name="visa" id="visa" />Visa</td>
</tr>
<tr>
<td><input type="submit" name="submit" id="submit" value="submit" /></td>
<td><input type="reset" name="reset" id="reset" value="reset" /></td>
</table>
</form>

</body>
</html>