Solved

validate drop down select boxes on submit with jquery

Posted on 2008-10-26
5
7,473 Views
Last Modified: 2012-06-27
Hi,

I'm trying to find out how you can use jQuery validator package to validate drop down select boxes. Say I have a list of countries (see snippet below). I need to make sure that a valid option was selected on submit. I.e. something other than "Select..."


<select name="searchCountry" id="searchCountry">
<option value="">Select country</option>
<option value="Afghanistan">Afghanistan</option>
<option value="Albania">Albania</option>
<option value="Algeria">Algeria</option>
<option value="American Samoa">American Samoa</option>
....
</select>
								
									<option value="Andorra"
										
									>Andorra</option>

Open in new window

0
Comment
Question by:vlad_oz
  • 2
  • 2
5 Comments
 
LVL 16

Expert Comment

by:sh0e
ID: 22810287

<script>
$(document).ready(function(){
$('form').submit(function(){
  var sel = $('#searchCountry option:selected').val();
  if(sel==''||sel==undefined||sel==null) alert('invalid choice');
});
});
</script>

Open in new window

0
 

Author Comment

by:vlad_oz
ID: 22810329
Hi sh0e,

Once again, thanks for the quick reply!

I can see how your solution will work, however, I'm after a solution using jquery validate plugin. I'd also need the error messages to be displayed next to the drop down rather than in a popup.

Cheers,
V
0
 
LVL 16

Accepted Solution

by:
sh0e earned 500 total points
ID: 22810344
<script>
$(document).ready(function(){
$('form').validate();
});
</script>

<select name="searchCountry" id="searchCountry" class="required">
<option value="">Select country</option>
<option value="Afghanistan">Afghanistan</option>
<option value="Albania">Albania</option>
<option value="Algeria">Algeria</option>
<option value="American Samoa">American Samoa</option>
....
</select>
0
 

Author Comment

by:vlad_oz
ID: 22864280
sh0e,
Once again, thanks for your help!
I spent some time googling how to use jQuery validation plugin, there seems to a lot of different ways to have it implemented, yet I couldn't find a single place which would explain all the different options and the best practice of implementing them. Your solution, i.e. class="required" is the simplest.
For everyone else's benefit one of the ways to add error mesages is to have a title attribute added to the select element e.g.
title="Please select a Country" class="required", by default the error messages will appear underneath the select, this behaviour can be changed.
also a good way to indicate to the users what is wrong with the form is to highlight the offending field(s), below is a small <style> snippet which will highlight the field in red. The $('#formid').validate() will then become:
$("#searchForm").validate(
            {
                highlight: function(element, errorClass) {
                    $(element).addClass(errorClass).parent().prev().children("select").addClass(errorClass);
                }
            }
            );
<style type="text/css">
        form.cmxform { width: 50em; }
 
        form.cmxform label.error {
            margin-left: auto;
            width: 250px;
        }
        form.cmxform input.submit {
            margin-left: 0;
        }
        em.error { color: black; }
        #warning { display: none; }
        select.error {
            border: 1px dotted red;
        }
        </style>

Open in new window

0
 

Expert Comment

by:ravish007
ID: 23042741
Keeping value blank as suggested:

<option value="">Select Option</option>

Doesn't seem to work with class="required:true"... is this a but with jquery.validation?  It allows me to submit my form with "Select Option" selected!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

803 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