jQuery Validation for Drop Down Box

By | October 21, 2012

Drop down box is also called as combo box, select box.
jQuery validation for drop down box (select box) is some what complicated when compared to text box validation, because in select box( combo box) more than one values are placed in between <select> tag.

 $(#year option:selected).val(); – gets the value of the selected drop down box value. “year” is the ID of drop down box i.e., <select>

    
[code type=html]

<html>
<head>
<script type=”text/javascript”
src=”http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js”>
</script>
<script type=”text/javascript”>
$(document).ready(function(){
$(“#submit”).click(function(){
var year = $(#year option:selected).val();
if(year == “”)
{
$(“#msg”).html(“Please select a year”);
return false;
}
});
});
</script>
</head>
<body>
<div id=”msg”></div>
<form method=”post” action=””>
<select id=”year”>
<option>1990 </option>
<option>1991 </option>
<option>1992 </option>
<option>1993 </option>
<option>1994 </option>
<option>1995 </option>
</select>

<input type=”submit” name=”submit” id=”submit”>
</form>
</body>

 </html

[/code]

2 thoughts on “jQuery Validation for Drop Down Box

  1. Mike

    Hi thanks for sharing your idea, I am having a problem with your code. I copied it into my local server and in my folder I included jqueryfile only and its not working, is there anything wrong I am doing, thanks in advance

    Reply

Leave a Reply to Mike Cancel reply

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