Home > ASP.Net > Validate ListBox

Validate ListBox


try this example to validate ListBox control using CustomValidator Control

<script type="text/javascript">
function ValidarListaSeleccionada(sender, args)
  args.IsValid = document.getElementById(sender.controltovalidate).options.length>0;

        <asp:ListBox ID="ListBox1" runat="server">
        <br />
        <asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="ValidarListaSeleccionada" OnServerValidate="CustomValidator1_ServerValidate"            ControlToValidate="ListBox1" ErrorMessage="Select One." ValidateEmptyText="True"></asp:CustomValidator><br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Send" />

And the server side function, just in case

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
args.IsValid = lBox.Items.Count > 0;

Hope this helps

Good Luck

Categories: ASP.Net Tags:
  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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: