Set the max number of items that can be selectable in ListBox in asp.net and c# using java scripts
<script type="text/javascript" language="javascript">
function onClientSelectedIndexChangingHandler(source, arguments) {
var listbox = document.getElementById('<%=ddlCOMMISION.ClientID%>');
var selectedCount = 0;
for (var index = 0; index < listbox.options.length; index++) {
if (listbox.options[index].selected)
selectedCount += 1;
}
if (selectedCount < 4)
arguments.IsValid = true;
else
arguments.IsValid = false;
}
</script>
<asp:ListBox ID="ddlCOMMISION" SelectionMode="Multiple" CausesValidation="True" Height="50px" runat="server">
<asp:ListItem>--Select--</asp:ListItem>
<asp:ListItem>% ON BASIC RATE</asp:ListItem>
<asp:ListItem>% ON NETT ORDER AMT</asp:ListItem>
<asp:ListItem>Rs PER KG</asp:ListItem>
</asp:ListBox>
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Maximum Countries you can select are 5"
ClientValidationFunction="onClientSelectedIndexChangingHandler"></asp:CustomValidator>