|
Hi,
I have a specific project which requires unique form validation. I have searched the web looking for a solution for a couple days to no avail. Can any of you javascript coders help point me in the right direction?
Here's what I need:
Within a FORM, we have to set a limit (25) on the total quantity of items ordered.
Each item has a drop-down menu with numbers 1 through 25.
What I need is that onSubmit, the sum of all selections would be added up. The total can not be more than 25. For example, a person could select 10 apples, 10 bananas, and 5 oranges.
If the user selected MORE than 25 items, a javascript alert box would pop up, and the form would not be submitted. |
|
Make sense?
Can anyone help me with this?
Vanessa
|
|
|
Copy & Paste this into a new HTML page, then open in your browser. It's a working example to do what you want. Enjoy.
<form>
apples<br>
<select name=apples>
<option value=5>5
<option value=10>10
<option value=15>15
<option value=20>20
<option value=25>25
</select>
<p>oranges<br>
<select name=oranges>
<option value=5>5
<option value=10>10
<option value=15>15
<option value=20>20
<option value=25>25
</select>
<p>bananas<br>
<select name=bananas>
<option value=5>5
<option value=10>10
<option value=15>15
<option value=20>20
<option value=25>25
</select>
<p><input type=button value="Order" onclick="Validate()">
</form>
<script language="javascript">
var f = document.forms[0];
function Validate() {
appleCnt = f.apples.options[f.apples.selectedIndex].value / 1;
orangeCnt = f.oranges.options[f.oranges.selectedIndex].value / 1;
bananaCnt = f.bananas.options[f.bananas.selectedIndex].value / 1;
itemCnt = appleCnt + orangeCnt + bananaCnt;
if (itemCnt > 25) {
alert("Sorry, your total item count\ncan not exceed 25.\n\nYou have "+itemCnt+" items selected.");
} else {
f.submit();
}
}
</script> |
|
|
|
|
|
|
|
|
|
|
|