Home > ASP.Net, Javascript > Validate ListBox (Part II)

Validate ListBox (Part II)


In a previous post i made an example to show how to validate ListBox using Server and Client Side with CustomValidator control , you can check this post from here Validate ListBox

Now i have another way to Validate ListBox using Client Side to allow user to at least one item in ASP.Net ListBox Control

<asp:ListBox ID="ListBox1" runat="server" SelectionMode = "Multiple">
    <asp:ListItem Text = "Egypt" Value = "1"></asp:ListItem>
    <asp:ListItem Text = "USA" Value = "2"></asp:ListItem>
    <asp:ListItem Text = "India" Value = "3"></asp:ListItem>
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Required"
ClientValidationFunction = "ValidateListBox"></asp:CustomValidator>

<asp:Button ID="Button1" runat="server" Text="Button" />

And the Javascript Code is :

<script type = "text/javascript">
function ValidateListBox(sender, args) {
    var options = document.getElementById("<%=ListBox1.ClientID%>").options;
    for (var i = 0; i < options.length; i++) {
        if (options[i].selected == true) {
            args.IsValid = true;
    args.IsValid = false;

Hope this helps

Good Luck.

Categories: ASP.Net, Javascript
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: