Solved

validate drop down select boxes on submit with jquery

Posted on 2008-10-26
5
7,471 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
Comment Utility

<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
Comment Utility
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
Comment Utility
<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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

771 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

10 Experts available now in Live!

Get 1:1 Help Now