title: Validating dropdownlist OR textbox
body: <p>I have a page where a user can either select a vendor via dropdown or enter a vendor number via textbox. One or the other must have a value. I can do this in javascript easily but how can I do this using a custom validator provided by ajax all on the client side?</p>
  
  <p><strong>Edited</strong></p>
  
  <pre><code> Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
      If Page.IsValid Then
          //save stuff
      End If
   End Sub
  
  Sub ServerValidation(ByVal source As Object, ByVal args As ServerValidateEventArgs) Handles ValidPage.ServerValidate
  
       Try
           ' Test whether the value entered into the text box is even.
           Dim num As Integer = Integer.Parse(args.Value)
           args.IsValid = ((num Mod 2) = 0)
  
       Catch ex As Exception
           args.IsValid = False
  End Try
  
  End Sub
  
  Protected Sub ValidPage_ServerValidate(ByVal source As Object, ByVal args As ServerValidateEventArgs)
      Try
          args.IsValid = (Not String.IsNullOrEmpty(Me.txtVendnum.Text) OrElse Me.DropDownList1.SelectedIndex &lt;&gt; 0)
      Catch e As Exception
          DirectCast(source, CustomValidator).ErrorMessage = "You must Choose a Vendor"
          args.IsValid = False
      End Try
  End Sub
  &lt;script type="text/javascript" language="javascript" &gt;
     function ClientValidate(sender, args) {
         // Get Both form fields
         var ddlvalue = document.getElementById('&lt;%=DropDownList1.ClientID%&gt;');
         var txtValue = document.getElementById('&lt;%=txtVendnum.ClientID %&gt;');
  
      // do you client side check to make sure they have something
         if (txtValue.value == '' &amp;&amp; ddlvalue.value == '0') {
  
          args.IsValid = false;
      }
      else
      {
          args.IsValid = true;
      }
  </code></pre>
  
  <p>}</p>
  
  <p></p>
  
  <pre><code>&lt;asp:DropDownList ID="DropDownList1" runat="server" 
                      DataSourceID="SqlDataSource1" ValidationGroup="Save" DataTextField="vmvnnm" DataValueField="vmvend" &gt;
                      &lt;/asp:DropDownList&gt;
  
  &lt;asp:TextBox ID="txtVendnum" ValidationGroup="Save" runat="server"&gt;&lt;/asp:TextBox&gt;&lt;br /&gt;
  
                          &lt;asp:CustomValidator ID="ValidPage" ValidationGroup="Save" runat="server" ClientValidationFunction="ClientValidate" ErrorMessage="CustomValidator" 
                              SetFocusOnError="True" ControlToValidate="txtVendnum" EnableClientScript="true" Display="Static" OnServerValidate = "ServerValidation" &gt;&lt;/asp:CustomValidator&gt;
  
  //other stuff. A different validator group, html editor, etc
  
  
  &lt;td&gt;
                          &lt;asp:ImageButton ID="ImageButton1" CausesValidation = "true"     ValidationGroup="Save" OnClick="ImageButton1_Click" runat="server" ImageUrl="../images/bttnSave.gif" /&gt;
  
                      &lt;/td&gt;
  </code></pre>