moe
moe

Reputation: 5249

How to check if checkbox that is in GridView is checked using JavaScript

I have Checkbox and text-box inside a GRIDVIEW. If checkbox is not checked and the text-box has no comments, then I want to show a message that says please enter comments in the text-box. I don’t want to show any message if all checkboxes are checked. I want to accomplish this by using a JavaScript so I have tried but I am not quite there yet and I had some issues all day with this. Please help. I am only checking the checkbox here and not the text-box and I am not sure how to check both the checkbox and the text-box so please help. Here is my JavaScript:

<script type="text/javascript">
          function validate() {
              var flag = true;
              var checkbox = new Array(); 
              var gridview = document.getElementById('<%=GridView1.ClientID%>');
              checkbox = gridview.getElementsByTagName('myCheckbox');
              for (var i = 0; i < checkbox.length; i++) {
                  if (checkbox.item(i).checked) 
                  {
                      flag = false;
                      break;
                  }
              }
              if (!flag) {
                  alert('Please enter comments.  Thanks');

              }
              return flag;
          }
</script>

and here is my checkbox and the text-box in the aspx file

<asp:TemplateField ItemStyle-Width="150px" HeaderText="Comments">
  <ItemTemplate>
  <asp:TextBox ID="txtComm" runat="server" TextMode="MultiLine" Width="130px" Height="50px"    BackColor="LightGoldenrodYellow"
   Text='<%# Eval("COMMENTS")%>'></asp:TextBox>
   </ItemTemplate>
  </asp:TemplateField>

     <asp:TemplateField ItemStyle-Width="15px" HeaderText="Approved?">
       <ItemTemplate>
      <asp:CheckBox ID="mycheckbox" runat="server"  Checked='<%#Eval("APPR")==DBNull.Value?  false:Eval("APPR") %>' />
        </ItemTemplate>
       </asp:TemplateField>

Upvotes: 2

Views: 18842

Answers (1)

afzalulh
afzalulh

Reputation: 7943

This should work: EDIT : Edited to reflect the requested change.

function validate() {
    var flag = false;
    var gridView = document.getElementById('<%= GridView1.ClientID %>');

    for (var i = 1; i < gridView.rows.length; i++) {
        var inputs = gridView.rows[i].getElementsByTagName('input');
        var areas = gridView.rows[i].getElementsByTagName('textarea');
        if (inputs != null && inputs.length > 1 && inputs[0] != null && areas != null && areas.length > 1 && areas[0] != null) {
            if (areas[1].type == "textarea" && inputs[0].type == "checkbox") {
                var txtval = areas[1].value;
                if (!inputs[0].checked && (txtval == "" || txtval == null)) {

                    flag = false;
                    break;
                }
                else {
                    flag = true
                }
            }
        }
    }
    if (!flag) {
        alert('Please enter comments.  Thanks');

    }
    return flag;
}

Upvotes: 3

Related Questions