How to check that the list box is not empty (client side)

I am working with ASP.NET 3.5. I have a list in which users should add items (I wrote code for this). My requirement is that at least one element must be added to the list or they cannot submit the form. I have several other validators on the page, and all of them are written to the ValidationSummary element. I would like this list check to also be written to the check control. Any help is appreciated. Thank.

+3
source share
7 answers

Remove the special validator, add the desired error message to it, double-click on the custom validator to get the code behind the event handler, and then you will implement the server command as follows:

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

You can also implement client-side javascript.

I just threw it on the page and quickly tested it, so you may need to tweak it a bit: (Button1 adds an item to the list box)

<script language="JavaScript">
<!--
  function ListBoxValid(sender, args)
  {
      args.IsValid = sender.options.length > 0;
  }
// -->
</script>    
<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" ValidationGroup="NOVALID" />
<asp:Button ID="Button2" runat="server" Text="ButtonsUBMIT"  />

<asp:CustomValidator ID="CustomValidator1" runat="server" 
ErrorMessage="CustomValidator" 
onservervalidate="CustomValidator1_ServerValidate" ClientValidationFunction="ListBoxValid"></asp:CustomValidator>

If you add a check summary to the page, the error text should appear in this summary if there are no items in the ListBox or other control that you want to use, unless the ValidationGroup is the same.

+5
source

This did not work for me:

function ListBoxValid(sender, args) 
{
        args.IsValid = sender.options.length > 0; 
}

But this happened:

function ListBoxValid(sender, args)
{
        var ctlDropDown = document.getElementById(sender.controltovalidate);
        args.IsValid = ctlDropDown.options.length > 0; 
}
+5
source

CustomValidator:

Display="Dynamic" ValidateEmptyText="True"
+3
<asp:CustomValidator 
     runat="server" 
     ControlToValidate="listbox1"
     ErrorMessage="Add some items yo!" 
     ClientValidationFunction="checkListBox"
/>

<script type="Text/JavaScript">
  function checkListBox(sender, args)
  {
      args.IsValid = sender.options.length > 0;
  }
</script>    
+1

( JavaScript).

ListBox.options.length , . , , - for .

function ListBoxValid(sender, args) {

  var listBox = document.getElementById(sender.controltovalidate);

  var listBoxCnt = 0;

  for (var x =0; x<listBox.options.length; x++)
  {
    if (listBox.options[x].selected) listBoxCnt++;
  }

  args.IsValid = (listBoxCnt>0)

}

>

+1

<script language="JavaScript">
  function CheckListBox(sender, args)
  {
      args.IsValid = document.getElementById("<%=ListBox1.ClientID%>").options.length > 0;
  }
</script>    
<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
<asp:CustomValidator ID="CustomValidator1" runat="server" 
ErrorMessage="*Required" ClientValidationFunction="CheckListBox"></asp:CustomValidator>
0

, ClientID. Microsoft AJAX .

-1

Source: https://habr.com/ru/post/1697466/


All Articles