Gridview checkbox validation

i m having a checkbox in gridview. i want to give validation if the checkbox is not selected that it prompt msg “select checkbox first” and
2)validation tht i want user to select maximum one check box from gridview ?
can any one help me for ths.

my checkbox look like this:
<asp:GridView ID=“gvExistingIncident” runat=“server” AutoGenerateColumns=“False”
Height=“155px” Width=“827px” UseAccessibleHeader=“False” OnSelectedIndexChanged=“gvExistingIncident_SelectedIndexChanged” ForeColor=“#333333” OnRowDataBound=“gvExistingIncident_RowDataBound” Font-Bold=“True” Font-Strikeout=“False”>
<HeaderStyle CssClass=“DataGridFixedHeader” />
<Columns>
<asp:TemplateField HeaderText=“Select”>
<ItemTemplate>
<asp:CheckBox ID=“ChkBx1” runat=“server” />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor=“LightGray” Height=“30px” />
</asp:GridView>


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


All above code you have to write in aspx page not in aspx.cs

Thanks,
kunalraj

i want to give another validation also that user can select maximun one checkbox from gridview… user cannot select 2 or more thn tht checkbox at a time. so how to do this.?

so for that write another function



var maxBoxes = 1;
  var boxesChecked = 0;

function checkIt(theBox)
{


    if (boxesChecked+1 > maxBoxes)
        {
            alert("please select only one checkbox");
            return false;
        }
    boxesChecked++
    return true;
}

and on chkbox write
<asp:CheckBox ID="CheckBox2" runat="server" onclick="if (this.checked) return checkIt(this); else boxesChecked--"/>


This will solve your problem

thanks,
kunalraj

now i want to give validation on particular textbox event…
i hav 1 textbox and radiobuttonlist…
so i want to give validation like if textbox in not empty it has some value and user is not selecting anything in radiobuttonlist and clickng on submitbutton at tht time i want to prompt msg like select radiobutton…

please give proper detail i can`t understand.

you have to give validation textbox if it is empty or select radio button atleast one from radiolist?

and both are bound each other?

thanks,
kunalraj

i dnt want to give for txtbox i want to give for radiobutton… in one of my form it is not compulsory to fill empid and recdvia…
empid is a textbox
and recdvia is a radiobuttonlist
so for user thes two field are not compulsory but if the user is entering empid and he/she must hav to select recdvia so i want to give validation on recdvia if the user is entering empid then he has to select the recdvia if he/she not selecting then vl prompt message that please select recdvia…


<script lang="javascript" type="text/javascript">
    function txtdata(){
            var val=document.getElementById("one_txt1").value;//one_txt1 is textboxname
            if(val==''){
                alert("please enter data");
                document.getElementById("one_txt1").focus();//one_txt1 is textboxname
                return false;
            }
            if(Validate_Radiobutton()== false){
                return false;
            }
                return true;
        }

      function Validate_Radiobutton()
        {
        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 radiobutton..!");
                return false;
            }

                return true;

        }
     </script>

and on button
<asp:Button ID="btn_save" runat="server" Style="z-index: 100; left: 0px; top: 0px"  Text="Save" OnClientClick="return txtdata()" />


this is solve ur prob

thanks,
kunalraj

Finlay this is solve your problem perfactly.


    <script lang="javascript" type="text/javascript">
     function txtdata(){
        var val=document.getElementById("one_txt1").value;[B]//one_txt1 is textbox name[/B]
            if(val!= "" && Validate_Radiobutton()== false){
                return false;
            }
                return true;
        }

      function Validate_Radiobutton()
        {
        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 radiobutton..!");
                return false;
            }
                return true;
        }

function checkIt(theBox)
{
  var maxBoxes = 0;
  var boxesChecked = 0;
  var val5=document.getElementById("one_txt1").value;[B]//one_txt1 is textbox name       [/B]
      if (boxesChecked+1 > maxBoxes)
        {
            if(val5 == ""){
                alert("please enter data in textbox then select.");
                return false;
            }else{
                return true;
            }
        }
    boxesChecked++
    return true;
}

</script>

If you have 3 radiobutton in radio button list then write below
<asp:RadioButtonList ID="RadioButtonList1" runat="server" Style="z-index: 100; left: 0px; top: 0px">
                        <asp:ListItem [B]onclick="if (this.checked) return checkIt(this); else boxesChecked--"[/B]>Employee</asp:ListItem>
                        <asp:ListItem [B]onclick="if (this.checked) return checkIt(this); else boxesChecked--"[/B]>Admin</asp:ListItem>
                        <asp:ListItem [B]onclick="if (this.checked) return checkIt(this); else boxesChecked--"[/B]>Supervisor</asp:ListItem>
                    </asp:RadioButtonList>

and on submit button click write
<asp:Button ID="btn_save" runat="server" Style="z-index: 100; left: 0px; top: 0px" Text="Save" [B]OnClientClick="return txtdata()"[/B] />


thanks,
kunalraj

this thread would be better suited for javascript forum and not asp.net

function checkIt(thebox) is not working and all other are working…

thanks i got the solution

ok