HI friends, in this article I will explain about how to
validate asp checkboxlist using JavaScript.
In previous article I already explained about How to insert multiple rows into the GridView without using any database in ASP.NET using VB.NET/C# || how to save multiple rows in GridView into a Session.,How to insert the data from the TextBox into the Grid View without using any database in ASP.NET using VB.NET/C# and How to repeater using SqlDataAdapter, DataTable and Stored procedure in Sql server using ASP.NET,VB.NET/C#
First open website or create one website. Take one new
webpage and design the webpage as shown in the below figure.
ASP.NET Code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Aspdotnet-Kishore:Validate
checkboxlist in ASP.NET using JavaScript</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h3>Aspdotnet-:Validate
checkboxlist in ASP.NET using JavaScript</h3>
Interests:<asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatDirection
="Horizontal" >
<asp:ListItem Value="Chess">Chess</asp:ListItem>
<asp:ListItem Value="Carooms">Carooms</asp:ListItem>
<asp:ListItem Value="Cricket">Cricket</asp:ListItem>
</asp:CheckBoxList><br />
<asp:Button ID="Button1" runat="server" Text="Submit" />
</div>
</form>
</body>
</html>
|
And write the below code in the <head> <script>
tag as shown below and give the function name in submit button OnClientClick.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Aspdotnet-Kishore:Validate
checkboxlist in ASP.NET using JavaScript</title>
<script type="text/javascript" language ="javascript"
>
function
validate()
{
var
listItems = document.getElementById("CheckBoxList1").getElementsByTagName("input");
var
itemcount = listItems.length;
var
iCount = 0;
var
isItemSelected = false;
for
(iCount = 0; iCount < itemcount; iCount++) {
if
(listItems[iCount].checked) {
isItemSelected = true;
break;
}
}
if
(!isItemSelected) {
alert("Please
select an Item.");
}
else
{
return
true;
}
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<h3>Aspdotnet-Kishore:Validate
checkboxlist in ASP.NET using JavaScript</h3>
Interests:<asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatDirection
="Horizontal" >
<asp:ListItem Value="Chess">Chess</asp:ListItem>
<asp:ListItem Value="Carooms">Carooms</asp:ListItem>
<asp:ListItem Value="Cricket">Cricket</asp:ListItem>
</asp:CheckBoxList><br />
<asp:Button ID="Button1" runat="server" Text="Submit" OnClientClick="return validate();"/>
</div>
</form>
</body>
</html>
|
No comments:
Post a Comment