Monday, December 17, 2012

Java Script Dropdownlist validation in asp.net

In previous post i have given Dropdownlist validation using jquery.In this post i will show how to perform validation on drop down list using java script in asp.net.Here we have to check whether the select index value of dropdown list is zero or not.Based on that we can do validation on it .
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DropdownValidation.aspx.cs" Inherits="DropdownValidation" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Dropdown Validation using Jquery</title>
<script language="javascript" type="text/javascript">
function cityValidate() {
var ddlcity = document.getElementById("<%=ddlCity.ClientID %>");
if (ddlcity.selectedIndex <= 0) {
alert('please choose city');
return false;
}
else
return true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<table><tr><td>First Name:</td><td> <asp:TextBox ID="txtname" runat="server"></asp:TextBox></td></tr>
<tr><td>Last Name:</td><td><asp:TextBox ID="txtlastname" runat="server"></asp:TextBox></td></tr>
<tr><td>Phone:</td><td>  <asp:TextBox ID="txtaddr" runat="server"></asp:TextBox></td></tr>
<tr><td>City</td><td><asp:DropDownList ID="ddlCity" runat="server">
<asp:ListItem Text="Please Select" Value=""></asp:ListItem>
<asp:ListItem Text="Hyderabad" Value="a"></asp:ListItem>
<asp:ListItem Text="Delhi" Value="b"></asp:ListItem>
<asp:ListItem Text="Bombay" Value="c"></asp:ListItem>
<asp:ListItem Text="Chennai" Value="c"></asp:ListItem>
</asp:DropDownList></td></tr></table>
<asp:Button ID="btnvalidate" runat="server" text="submit"  OnClientClick="return cityValidate()"/>
</form>
</body>
</html>
Basic Form
Alert for Dropdown validation
Recently i have seen a question regarding dropdown lists.i:e which is correct in drop down or drop-down?. Every one would provide the answer with comment on this post

No comments:

Bel