HOW TO CHECK ATLEAST ONE CHECKBOX IS CHECKED?

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
 1148  View(s)
Ratings:
Rate this:

I want to check at least one checkbox should be checked before unload the page or clicking on btn save.

<asp:GridView ID="grdTest" runat="server" DataKeyNames="Id" AutoGenerateColumns="False" HorizontalAlign="Center" GridLines="None" onrowcreated="grdTest_RowCreated">

  <asp:TemplateField >

      <ItemTemplate>

        <asp:TextBox ID="txtText" Rows="10" Columns="40" runat="server" TextMode="MultiLine"   Height="55px" Text='<%# Eval("Text") %>'></asp:TextBox>

        <asp:Button ID="btnSave" runat="server" Text="Save" CommandName="Save"/>           

        <span id="checkbox">

        <asp:RadioButton ID="rbtnTest"  runat="server" Checked='<%# Eval("Correct") %>' TextAlign="Left"  Height="28px" />

        </span>

      </ItemTemplate>

    <EditItemTemplate>

        <asp:TextBox ID="txtText" Rows="10" Columns="40" runat="server" TextMode="MultiLine" />

        <asp:Button ID="btnSave" runat="server" Text="Save" CommandName="Save"/>

        <asp:CheckBox ID="rbtnTest" runat="server"  Enabled="true" TextAlign="Left"/>

    </EditItemTemplate>

  </asp:TemplateField>

</asp:GridView>

plz some one help me?

  1. Sumit Kesarwani

    Post:378

    Points:2694
    Re: How to check atleast one checkbox is checked?

    Hi Ankit, 

    try this:

    <script type="text/javascript" language="javascript">

    function Validate_Checkbox()

    {

        var chks=document.getElementsByTagName('input');  

        var hasChecked = false;

        for (var i = 0; i < chks.length; i++)

            {

                if (chks[i].checked)

                    {

                        hasChecked = true;

                        break;

                    }

            }

          if (hasChecked == false)

                {

                    alert("Please select at least one checkbox..!");

                    return false;

                }

                return true;

    }    

    </script>

    and on Submit Button you have to write

    OnClientClick="return Validate_Checkbox()"

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!