Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Dropdownlist Validation using JavaScript in HTML, Asp.net

Dec 29, 2014
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.


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


<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>
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
  

<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>
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


Select Education:

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.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

3 comments :

Anonymous said...

Thanks...!!!

Unknown said...

Thank you

Unknown said...

what is clientid

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.