Home > DeveloperSection > Forums > How to check atleast one checkbox is checked?
Ankit Singh

Total Post:341

Points:2389
Posted on    September-02-2014 12:15 AM

 ASP.Net
Ratings:


 1 Reply(s)
 814  View(s)
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?



Sumit Kesarwani

Total Post:378

Points:2694
Supported
Posted on    September-02-2014 12:42 AM

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()"


Don't want to miss updates? Please click the below button!

Follow MindStick