title: get all selected values from ListBox (client side)
body: <p>I'm trying to implement a custom validator in JavaScript for my vb.net page. This validator should check if in a multichoice listbox there aren't any selected values, showing an error pop up if so.</p>
  
  <p>The thing is, I want to do it client side, but in my 'validateFunction' function I only get the last selected (or unselected, if it was selected already) item. I know how to do it in code-behind code, but I want to do it client-side.</p>
  
  <p><em>aspx code:</em></p>
  
  <pre><code> &lt;asp:ListBox ID="lbEdit" runat="server" SelectionMode="Multiple"&gt;
   &lt;/asp:ListBox&gt;
   &lt;asp:CustomValidator id="cvEdit" runat="server" Display="None" ControlToValidate="lbEdit" ClientValidationFunction="validateFunction"/&gt;
   &lt;ajax:ValidatorCalloutExtender runat="server" ID="vceEdit" TargetControlID="cvEdit" /&gt;
  </code></pre>
  
  <p><em>JavaScript code:</em></p>
  
  <pre><code> function validateFunction(source, arguments) {
          var options = document.getElementById(source.controltovalidate).options;
              for (var i = 0; i &lt; options.length; i++) {
                  if (options[i].selected == true) {
                      args.IsValid = true;
                      return;
                  }
              }
              args.IsValid = false;
   }
  </code></pre>
  
  <p>As I said, since last (un)selected item will be the only one selected in the 'options' array, the validating function will always return true... </p>
  
  <p>I thought of populating another array in another javascript function as the 'true selected array' and compare its values with the selected option each time the function fires.. but I think there should be a better way.</p>
  
  <p>So... Is there a straight forward way to get ALL the selected items from a multichoice listbox in javascript? </p>