title: How can I extend the functionality of this script?
body: <p>The following script allows a user to either fill out the textboxes in a gridview control with a checkbox called <strong>grid1Details</strong>.</p>
  
  <p>If the user chooses not to fill out the textboxes, then the user must check the <strong>grid1Details</strong> checkbox in other to submit the form.</p>
  
  <p>If the user does not put a checkmark on the checkbox or does not enter values into all three textboxes, then the user gets our custom alert message <strong><em>"Please enter values on all textboxes or check the checkbox next to each textbox."</em></strong> </p>
  
  <p>This works great and here is that script:</p>
  
  <pre><code>      &lt;script type="text/javascript"&gt;
            function validate() {
                var checkbox = document.getElementById("&lt;%=grid1Details.ClientID %&gt;");
                var txtsourcename = $("[id*='txtsourcename']")[0];
                var txtsourceaddress = $("[id*='txtsourceaddress']")[0];
                var txtsourceincome = $("[id*='txtsourceincome']")[0];
  
                if (checkbox.checked) {
                    if ($(txtsourcename).val().length != 0 &amp;&amp; $(txtsourceaddress).val().length != 0 &amp;&amp; $(txtsourceincome).val().length != 0) {
                        jAlert("Please enter values on all textboxes or check the checkbox next to each textbox.");
                        return false;
                    } else {
                        return true;
                    }
                } else {
                    if ($(txtsourcename).val().length != 0 &amp;&amp; $(txtsourceaddress).val().length != 0 &amp;&amp; $(txtsourceincome).val().length != 0) {
                        return true;
                    } else {
                        jAlert("Please enter values on all textboxes or check the checkbox next to each textbox.");
                        return false;
                    }
                }
             }
     &lt;/script&gt;
  </code></pre>
  
  <p>The issue we are having now is that we have several gridview controls, each with its own checkbox control and must be validated the same way.</p>
  
  <p>In the next script, we are trying to add <strong>spouseDetails</strong> checkbox but it is not working.</p>
  
  <p>Each time, we either enter data into the two checkbox controls, the <strong>grid1Details</strong> control and <strong>spouseDetails</strong> checkbox control and hit the submit button, it doesn't validate the <strong>spouseDetails</strong> checkbox.</p>
  
  <p>The <strong>grid1Details</strong> checkbox gets validated but the <strong>spouseDetails</strong> checkbox is ignored.</p>
  
  <p>Any ideas what we are doing wrong?</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      function validate() {
          var checkbox = document.getElementById("&lt;%=grid1Details.ClientID %&gt;");
          var txtsourcename = $("[id*='txtsourcename']")[0];
          var txtsourceaddress = $("[id*='txtsourceaddress']")[0];
          var txtsourceincome = $("[id*='txtsourceincome']")[0];
  
          //spouse check
          checkbox = document.getElementById("&lt;%=spouseDetails.ClientID%&gt;");
          var txtspousename = $("[id*='txtspousename']")[0];
          var txtspouseaddress = $("[id*='txtspouseaddress']")[0];
          var txtspouseincome = $("[id*='txtspouseincome']")[0];
  
          if (checkbox.checked) {
              if ($(txtsourcename).val().length != 0 &amp;&amp; $(txtsourceaddress).val().length != 0 &amp;&amp; $(txtsourceincome).val().length != 0) {
                  jAlert("Please enter values on all textboxes or check the checkbox next to each textbox.");
                  return false;
              } else {
                  return true;
              }
              if ($(txtspousename).val().length != 0 &amp;&amp; $(txtspouseaddress).val().length != 0 &amp;&amp; $(txtspouseincome).val().length != 0) {
                  jAlert("Please enter values on all textboxes or check the checkbox next to each textbox.");
                  return false;
              } else {
                  return true;
              }            
          } else {
              if ($(txtsourcename).val().length != 0 &amp;&amp; $(txtsourceaddress).val().length != 0 &amp;&amp; $(txtsourceincome).val().length != 0) {
                  return true;
              } else {
                  jAlert("Please enter values on all textboxes or check the checkbox next to each textbox.");
                  return false;
              }
              if ($(txtspousename).val().length != 0 &amp;&amp; $(txtspouseaddress).val().length != 0 &amp;&amp; $(txtspouseincome).val().length != 0) {
                  return true;
              } else {
                  jAlert("Please enter values on all textboxes or check the checkbox next to each textbox.");
                  return false;
              }            
          }
      }
     &lt;/script&gt;
  
  
  //click button:
        &lt;td&gt; &lt;asp:Button ID="btnNext" CssClass="btnNext" runat="server" Text=" Review Form " OnClientClick="BtnClick(); return validate()" OnClick="btnNext_Click" /&gt;&lt;/td&gt;
  
  
   &lt;table&gt;
      &lt;tr&gt;
          &lt;td&gt;
              &lt;asp:CheckBox ID="grid1Details" ClientIDMode="Static" runat="server" Checked="false" AutoPostBack="true" OnCheckedChanged="Grid1CheckChanged" /&gt;&lt;span style="color:#ff0000"&gt;*&lt;/span&gt;
          &lt;/td&gt;
          &lt;td&gt;
            &lt;asp:gridview ID="Gridview1" gridlines="None" runat="server" ShowFooter="true" AutoGenerateColumns="false" onrowdatabound="Gridview1_RowDataBound" OnRowDeleting="Gridview1_RowDeleting"&gt;
              &lt;Columns&gt;
              &lt;asp:BoundField DataField="RowNumber" Visible="false" HeaderText="Row Number" /&gt;
              &lt;asp:TemplateField HeaderText="Name"&gt;
               &lt;headerstyle horizontalalign="Left" /&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:TextBox ID="txtsourcename" placeholder="Name..." runat="server" style="width:250px;" class="form-control"&gt;&lt;/asp:TextBox&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
              &lt;asp:TemplateField HeaderText="Address"&gt;
              &lt;ItemStyle HorizontalAlign="Left"&gt;&lt;/ItemStyle&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:TextBox ID="txtsourceaddress" placeholder="Address..." runat="server" style="width:250px;" class="form-control"&gt;&lt;/asp:TextBox&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
              &lt;asp:TemplateField HeaderText="Income"&gt;
              &lt;ItemStyle HorizontalAlign="Left"&gt;&lt;/ItemStyle&gt;
                  &lt;ItemTemplate&gt;
                       &lt;asp:TextBox ID="txtsourceincome" placeholder="Income...(example: 1000)" runat="server" style="width:250px;" class="form-control txtsourceincome numeric"&gt;&lt;/asp:TextBox&gt;
                  &lt;/ItemTemplate&gt;
                         &lt;/asp:TemplateField&gt;
              &lt;asp:TemplateField HeaderText=""&gt;
                  &lt;ItemTemplate&gt;
                   &lt;asp:Button ID="ButtonAdd" runat="server" Text="Add" 
                          onclick="ButtonAdd_Click" CssClass="grvAddButton" OnClientClick="return ValidateEmptyValue();return validate()" /&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
             &lt;asp:CommandField ShowDeleteButton="True"&gt;&lt;ControlStyle CssClass="grvDelButton" /&gt;&lt;/asp:CommandField&gt;
              &lt;/Columns&gt;
            &lt;/asp:gridview&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  &lt;table&gt;
      &lt;tr&gt;
          &lt;td&gt;
              &lt;asp:CheckBox ID="spouseDetails" ClientIDMode="Static" runat="server" Checked="false" AutoPostBack="true" OnCheckedChanged="SpouseCheckChanged" /&gt;&lt;span style="color:#ff0000"&gt;*&lt;/span&gt;
          &lt;/td&gt;
          &lt;td&gt;                     
              &lt;asp:gridview ID="grvspouse" GridLines="None" runat="server" ShowFooter="true" AutoGenerateColumns="false" onrowdatabound="grvspouse_RowDataBound" OnRowDeleting="grvspouse_RowDeleting"&gt;
              &lt;Columns&gt;
              &lt;asp:BoundField DataField="SpouseNumber" Visible="false" HeaderText="Row Number" /&gt;
              &lt;asp:TemplateField HeaderText="Name"&gt;
              &lt;headerstyle horizontalalign="Left" /&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:TextBox ID="txtspousename" placeholder="Name..." runat="server" style="width:250px;" class="form-control"&gt;&lt;/asp:TextBox&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
              &lt;asp:TemplateField HeaderText="Address"&gt;
              &lt;ItemStyle HorizontalAlign="Left"&gt;&lt;/ItemStyle&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:TextBox ID="txtspouseaddress" placeholder="Address..." runat="server" style="width:250px;" class="form-control"&gt;&lt;/asp:TextBox&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
              &lt;asp:TemplateField HeaderText="Income"&gt;
              &lt;ItemStyle HorizontalAlign="Left"&gt;&lt;/ItemStyle&gt;
                  &lt;ItemTemplate&gt;
                       &lt;asp:TextBox ID="txtspouseincome" placeholder="Income...(example: 1000)" runat="server" style="width:250px;" class="form-control txtsourceincome numeric"&gt;&lt;/asp:TextBox&gt;
                  &lt;/ItemTemplate&gt;
             &lt;/asp:TemplateField&gt;
              &lt;asp:TemplateField HeaderText=""&gt;
                  &lt;ItemTemplate&gt;
                   &lt;asp:Button ID="ButtonAdd2" runat="server" Text="Add" 
                          onclick="ButtonAdd2_Click" CssClass="grvAddButton" OnClientClick="return ValidateSPEmptyValue();" /&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
             &lt;asp:CommandField ShowDeleteButton="True"&gt;&lt;ControlStyle CssClass="grvDelButton" /&gt;&lt;/asp:CommandField&gt;
              &lt;/Columns&gt;
            &lt;/asp:gridview&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
    &lt;td&gt; &lt;asp:Button ID="btnNext" CssClass="btnNext" runat="server" Text=" Review Form " OnClientClick="BtnClick(); return validate()" OnClick="btnNext_Click" /&gt;&lt;/td&gt;
  </code></pre>