2

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>

1 Answer 1

3

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;
}
Sign up to request clarification or add additional context in comments.

8 Comments

thanks for your quick response but i don't see where you are checking if the text-box has any comments. I want to check if the checkbox is not checked and the textbox is blank then show message. thanks
thank you so much, i will test it and let you know soon
i just tested and am just getting the message every time even if the condition is true or false. Here is the condition, if the checkbox is checked then i don't need any comments entered in the text-box BUT, if the checkbox is not checked then comments is required. I am almost there so please help. thanks
Sorry, forgot that you have textarea, not textbox. Please check the edited code. It works fine in test.
sorry i forgot to mention that i have 2 text-boxes and 2 checkboxes how would the code know which one is it checking? Here is the order of the controls in my gridview: dropdown textbox1 textbox2 checkbox1 checkbox2. i want to check textbox2 and checkbox1. thanks
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.