Link to home
Start Free TrialLog in
Avatar of polobruce
polobruce

asked on

jQuery multiple select validation

I have a e-commerce ticketing app that has multiple ticket types... each with a select drop down for quantity i.e. 0 - 10...  

How do I validate that someone has selected a quantity of 1 or greater for at least 1 of the select drop downs.


Example of the select field is below.
 
 
ADULT
	<select name="ticket[1].qty">
 
									  <option value="0">0</option>	
									  
									  <option value="1">1</option>
									  
									  <option value="2">2</option>
									  
									  <option value="3">3</option>
									  
									  <option value="4">4</option>
									  
									  <option value="5">5</option>
 
									  
									  <option value="6">6</option>
									  
									  <option value="7">7</option>
									  
									  <option value="8">8</option>
									  
									</select>
 
 
 
STUDENT
<select name="ticket[2].qty">
 
									  <option value="0">0</option>	
									  
									  <option value="1">1</option>
									  
									  <option value="2">2</option>
									  
									  <option value="3">3</option>
									  
									  <option value="4">4</option>
									  
									  <option value="5">5</option>
 
									  
									  <option value="6">6</option>
									  
									  <option value="7">7</option>
									  
									  <option value="8">8</option>
									  
									</select>

Open in new window

Avatar of anoyes
anoyes
Flag of United States of America image

A simple solution would be to grab the values of the two select boxes and make sure they're not both 0.  The example below assumes that you've got these two select elements inside a form and that you want to validate before the form gets submitted.

jQuery(function(($) {
 $('form').submit(function () {
  var a = $("select[name='ticket\[1\].qty']").val();
  var s = $("select[name='ticket\[2\].qty']").val();
  if (a == 0 && s == 0)
  {
    alert('Please order at least one ticket);
    return false;
  }
  return true;
 });
});

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of anoyes
anoyes
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial