<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function ValidateListBox(sender, args) {
var listBox = document.getElementById("<%=ListBox1.ClientID %>");
var options = listBox.getElementsByTagName("option");
var count = 0;
for (var i in options) {
if (options[i].selected) {
count++;
}
}
args.IsValid = count == 4;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ListBox ID="ListBox1" runat="server" SelectionMode = "Multiple">
<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:ListItem Text="Four" Value="4"></asp:ListItem>
<asp:ListItem Text="Five" Value="5"></asp:ListItem>
<asp:ListItem Text="Six" Value="6"></asp:ListItem>
</asp:ListBox>
<asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction = "ValidateListBox" ErrorMessage="Select 4 Items"></asp:CustomValidator>
<asp:Button ID="Button1" runat="server" Text="Button" CausesValidation = "true" />
</form>
</body>
</html>