jQuery validation plugin: required select elements

I’m just posting this as a reminder to myself, but it may help others! I found that when I added class=”required” to a select element in a form, it wasn’t actually treated as required by jQuery. After some fiddling about, I discovered that you can easily force it to be required by ensuring that you have a blank first option with a value of an empty string (zero will not do!). In other words:
<option value="" selected="selected"></option>
Note that I forced this option to be selected if I hadn’t passed a valid default option to be preselected when the form was loaded. This means that if the empty option is still selected (or has been chosen by the user) when the form is submitted, jQuery will detect it as required and display the appropriate error.

This entry was posted in Javascript and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>