View Full Version : Checking that at least one item is selected in loop generated list JavaScript

04-13-2015, 06:42 AM
I have a list generated from a Query with name, quantity and price creating a comma deliminated list that contains a hyphen deliminated list. works great.

The issue is that I need to confirm that at least one item in any of the rows has been selected before visitor can post page.

This is the select statement that generates the list:

<select name="quanty" style="width: 45px">
<cfloop from="0" to="#qnum#" index="quant">
<cfif quant LT 10>
<option value="#ssID#-#quant#-#gtserv2.price#">&nbsp;&nbsp;#quant</option>
<option value="#ssID#-#quant#-#gtserv2.price#">#quant#</option>

This is what the variable would look like if quantity of 1 was selected for the first item... 1801-1-1200.00,1802-0-250.00,1805-0-150.00,1806-0-100.00,1820-0-50.00

I am looping through a query where each result constitutes it's own form input and it's name is the service id. Sometimes the list can be several items and sometimes 20 or more. Since "0" is the default of each in5659put I need to know if one or more fields are larger than 0 and if none are so to trigger an JavaScript message saying "You must select at least one item".. Remember most inputs in the loop will remain as 0.

Right now I just check on the form to page and tell them to go back and select something. Seems like this would generate so much JavaScript that the page would drag but I wonder if there is a simple solution.


04-13-2015, 10:40 AM
1. Set a variable to 0
2. Split the string into an array using str.split(",");
3. Loop through the array and split each member using str.split("-");
4. Add the value of member[1] in each sub-array to the variable in line 1
5. On exit from the loop, the variable set in line 1 contains the total number of items selected