In this article I will explain how to validate (check) HTML Select DropDownList using JavaScript and jQuery.
Validate (Check) HTML Select DropDownList using jQuery
The following HTML Markup consists of an HTML Select DropDownList and a Button. The HTML Button has been assigned a jQuery OnClick event handler.
Inside the jQuery OnClick event handler, the HTML Select DropDownList object is referenced and if the selected value matches the value of the default item then an error message is displayed using JavaScript alert message box.
Select Fruit:
<select id="ddlFruits">
<option value=""></option>
<option value="1">Apple</option>
<option value="2">Mango</option>
<option value="3">Orange</option>
</select>
<input type="submit" id="btnSubmit" value="Validate" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("#btnSubmit").click(function () {
var ddlFruits = $("#ddlFruits");
if (ddlFruits.val() == "") {
//If the "Please Select" option is selected display error.
alert("Please select an option!");
return false;
}
return true;
});
});
</script>
Screenshot
Browser Compatibility
The above code has been tested in the following browsers.
* All browser logos displayed above are property of their respective owners.
Demo
Downloads
HTML_DropDownList_Validate.zip