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>


    

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<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>
<div> </html</div>

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

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

overappreciative-complexional