Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

validate drop down select boxes on submit with jquery

Posted on 2008-10-26
5
Medium Priority
?
7,482 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 1500 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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)
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…
Suggested Courses

916 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