Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Good Afternoon Developer, this is my content in asp .net, I want the user can check only 2 checkbox,exceeding that alert box should pop up.

C#
<body>
       <form id="form1" runat="server">
       <div>
           <table width="100%">
               <tr>
                   <td>
                   <asp:CheckBox id="q3a" runat="server" Text="Public" />
                   </td>
                   <td>
                   <asp:CheckBox id="q3b" runat="server" Text="void" />
                   </td>
                   <td>
                   <asp:CheckBox id="q3c" runat="server" Text="protected"/>
                   </td>
                   <td>
                   <asp:CheckBox id="q3d" runat="server" Text="return" />
                   </td>
               </tr>
           </table>
           <asp:Button ID="btnSubmit" Text="submit" runat="server"/>
       </div>
       </form>
   </body>


how can i write javascript for this, i have tried but can't find any way out, Pls help...
Posted

XML
<asp:CheckBoxList ID="CheckBoxList1" runat="server" >
            <asp:ListItem Text = "One" Value = "1"></asp:ListItem>
            <asp:ListItem Text = "Two" Value = "2"></asp:ListItem>
            <asp:ListItem Text = "Three" Value = "3"></asp:ListItem>
            <asp:ListItem Text = "Four" Value = "4"></asp:ListItem>
        </asp:CheckBoxList>
        <asp:Button ID="btn" runat="server" OnClientClick="return Validate()" Text="Click" />



XML
<script language="javascript" type="text/javascript">
    function Validate() {
        var CHK = document.getElementById("<%=CheckBoxList1.ClientID%>");
        var checkbox = CHK.getElementsByTagName("input");
        var counter = 0;
        for (var i = 0; i < checkbox.length; i++) {
            if (checkbox[i].checked) {
                counter++;
            }
        }
        if (counter > 2) {
            alert("You can select maximum 2 checkboxes");
            return false;
        }
        else {
            return true;
        }
        //alert(counter);
    }
</script>
 
Share this answer
 
funtion CheckValidation()
{
  var  chk1=document.getElementById('<%=q3a.ClientID%>');
 var chk2=document.getElementById('<%=q3a.ClientID%>');
  var chk3=document.getElementById('<%=q3a.ClientID%>');

   var chk4=do cument.getElementById('<%=q3a.ClientID%>');
var count=0;
  
if(chk1.checked)
{
count++;
}
if(chk2.checked)
{
count++;
}
if(chk3.checked)
{
count++;
}

if(chk4.checked)
{
count++;
}
if(count>2)
{
//do your logic here
}

}

call this funtion onselect /onchange event on the checkbox
<asp:checkbox id="q3a" runat="server" onchange="CheckValidation()" text="Public" xmlns:asp="#unknown" />
 
Share this answer
 
Comments
Reesha Santhosh 16-Mar-13 16:56pm    
When I used Solution 1, I got an error message that read - "ASP.default_aspx' does not contain a definition for 'CheckValidation' and no extension method 'CheckValidation' accepting a first argument of type 'ASP.default_aspx' could be found (are you missing a using directive or an assembly reference?)"
Can anyone tell me why this is occuring? I am pasting below the code.

<script language="javascript" type="text/javascript">
function CheckValidation() {
var chk1 = document.getElementById("<%=CheckBox1.ClientID%>");
var chk2 = document.getElementById("<%=CheckBox2.ClientID%>");
var chk3 = document.getElementById("<%=CheckBox3.ClientID%>");
var chk4 = document.getElementById("<%=CheckBox4.ClientID%>");
var count = 0;

if(chk1.checked)
{
count++;
}
if(chk2.checked)
{
count++;
}
if(chk3.checked)
{
count++;
}

if(chk4.checked)
{
count++;
}
if(count>2)
{
alert("You can select upto 2 checkboxes");
}
}
</script>





<br />

<asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="CheckValidation()" Text="Text1"
AutoPostBack="True" />
<br />
<asp:CheckBox ID="CheckBox2" runat="server" OnCheckedChanged="CheckValidation()" Text="Text2"
AutoPostBack="True"/>
<br />
<asp:CheckBox ID="CheckBox3" runat="server" OnCheckedChanged="CheckValidation()"
Text="Text3"
AutoPostBack="True"/>
<br />
<asp:CheckBox ID="CheckBox4" runat="server" OnCheckedChanged="CheckValidation()"
Text="Text4" AutoPostBack="True"
/>
<br />

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900