Solved

jQuery multiple select validation

Posted on 2009-04-06
2
1,665 Views
Last Modified: 2012-08-14
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

0
Comment
Question by:polobruce
  • 2
2 Comments
 
LVL 16

Expert Comment

by:anoyes
ID: 24091655
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

0
 
LVL 16

Accepted Solution

by:
anoyes earned 250 total points
ID: 24091661
Forgot a closing quote in there.  This should be the script:

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

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Autoplay video in mobile device 3 108
Jquery Autocomplete PHP script 3 37
Automatically open all pdf links in new tab 2 38
Angular - ng-if worked but why like this? 5 31
by Julian Matz As of jQuery (http://jquery.com/) 1.4 the .live() method (http://api.jquery.com/live/) supports custom events as well as some standard JavaScript events that it previously didn't support. Among these is the submit event. Unlike …
Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now