Click here to Skip to main content
12,945,927 members (55,131 online)
Rate this:
 
Please Sign up or sign in to vote.
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.

<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 13-Mar-12 23:17pm
The Doer2.7K
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

<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" />



<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>
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

 
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" />
  Permalink  
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)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 4,899
CHill60 3,205
Maciej Los 2,453
Jochen Arndt 1,935
ppolymorphe 1,795


Advertise | Privacy | Mobile
Web02 | 2.8.170518.1 | Last Updated 14 Mar 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100