Introduction:
Here I will explain how to validate dropdownlist using JavaScript in asp.net or html or dropdownlist validation using JavaScript in html or asp.net. To validate dropdownlist first we need to get selected value of dropdown based on that we can raise validation message.
Description:
In previous articles I explained how to remove row in table using jQuery, jQuery highlight table row and column on mouseover in asp.net, jQuery detect caps lock on or not, jQuery change image on mouseover, jQuery dropdown with images, jQuery Countdown timer script example and many articles relating to jQuery and asp.net. Now I will explain how to validate dropdownlist using JavaScript in asp.net or html.
In previous articles I explained how to remove row in table using jQuery, jQuery highlight table row and column on mouseover in asp.net, jQuery detect caps lock on or not, jQuery change image on mouseover, jQuery dropdown with images, jQuery Countdown timer script example and many articles relating to jQuery and asp.net. Now I will explain how to validate dropdownlist using JavaScript in asp.net or html.
Validate
Dropdownlist
To validate dropdownlist we will write code like as shown
below here we will get dropdownlist selected value and check if it matching
with first option value because first option value if we give it as “zero” we
can compare value easily and raise validation
|
If
you want to check it in complete example we need to write the code like as
shown below
Validate Dropdownlist using Javascript in
HTML:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Validate Dropdownlist using JavaScript</title>
<script type="text/javascript">
function ValidateDropdown() {
var result = document.getElementById('ddlEducation').value;
if (result == "0")
{
alert("Please
Select Education");
}
else {
alert("Dropdownlist
Selected Value is: " + result);
}
}
</script>
</head>
<body>
<form id="form1">
<div>
Select Education:
<select id="ddlEducation">
<option value="0">--Select Education--</option>
<option value="B.Tech">B.Tech</option>
<option value="MCA">MCA</option>
<option value="MBA">MBA</option>
<option value="AGBSC">AGBSC</option>
<option value="MBBS">MBBS</option>
</select>
<input type="button" value="Validate Dropdown" onclick="ValidateDropdown()"
/>
</div>
</form>
</body>
</html>
|
In case if you are using asp.net
dropdownlist control we can use same code but only
getting dropdownlist value in JavaScript code will change because in JavaScript
we can identify asp.net
controls by adding ClientID like as
shown below
|
If you want to check it in complete example you need
to write the code like as shown below
Validate Dropdownlist using Javascript in Asp.net:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Validate Dropdownlist using JavaScript</title>
<script type="text/javascript">
function ValidateDropdown() {
var result = document.getElementById('<%=ddlEducation.ClientID%>').value;
if (result == "0")
{
alert("Please
Select Education");
}
else {
alert("Dropdownlist
Selected Value is: " + result);
}
}
</script>
</head>
<body>
<form id="form1"
runat="server">
<div>
Select Education:
<asp:DropDownList ID="ddlEducation"
runat="server">
<asp:ListItem Value="0" Text="--Select Education--" />
<asp:ListItem Value="B.Tech" Text="B.Tech" />
<asp:ListItem Value="MCA" Text="MCA" />
<asp:ListItem Value="MBA" Text="MBA" />
<asp:ListItem Value="AGBSC" Text="AGBSC" />
<asp:ListItem Value="MBBS" Text="MBBS" />
</asp:DropDownList>
<input type="button" value="Validate Dropdown" onclick="ValidateDropdown()"
/>
</div>
</form>
</body>
</html>
|
Live
Demo
If
you want to check live demo click on below button
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
3 comments :
Thanks...!!!
Thank you
what is clientid
Note: Only a member of this blog may post a comment.