Validate ASP.Net CheckBoxList control using JavaScript.

vishalneeraj-24503
Posted by vishalneeraj-24503 under JavaScript category on | Points: 40 | Views : 1000
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem Text = "One" Value = "1"></asp:ListItem>
<asp:ListItem Text = "Two" Value = "2"></asp:ListItem>
<asp:ListItem Text = "Three" Value = "3"></asp:ListItem>
</asp:CheckBoxList>

<script type = "text/javascript">
var atLeast = 1

function Validate()
{
var CHK = document.getElementById("<%=CheckBoxList1.ClientID%>");
var checkbox = CHK.getElementsByTagName("input");
var counter=0;

for (var i=0;i<checkbox.length;i++)
{
if (checkbox[i].checked)
{
counter++;
}
}

if(atLeast>counter)
{
alert("Please select atleast " + atLeast + " item(s)");

return false;
}
return true;
}
</script>

Comments or Responses

Login to post response