codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Form - setting limits & summing values in drop down menus  vanessa19 at 16:40 on Friday, October 01, 2004
 

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

  Re: Form - setting limits & summing values in drop down menus  Troy Wolf at 03:36 on Monday, October 04, 2004
 

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>
Troy Wolf: site expert
Shiny Solutions









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  How to dynamically create a calendar when I click a button?
•  c++ tutorials
•  Update a second table
•  Trackin Site behaviour
•  variable scope problem
•  dynamic crystal report generation
•  execute an Excel macro when the sheet is changed from ASP.NET
•  Re: form
•  form


Recent Articles
Communicating with the Database (Using ADO)
MagicGrid
Simple Thumbnail Browsing Solution
Type Anywhere
A Better Moustrap: FmtDate to replace FormatDateTime
ASP.NET Forum Source Code
Internal Search Engine
Javascript Growing Window
Simple date validation
Search engine friendly URLs using ASP.NET (C#.NET)


Site Survey
Help us serve you better. Take a five minute survey. Click here!

© Copyright codetoad.com 2001-2005