Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET Javascript
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 14-Mar-12 0:17am
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 at 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
0 Mathew Soji 334
1 Maciej Los 295
2 BillWoodruff 290
3 Afzaal Ahmad Zeeshan 257
4 DamithSL 225
0 OriginalGriff 6,369
1 Sergey Alexandrovich Kryukov 5,973
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,045


Advertise | Privacy | Mobile
Web02 | 2.8.1411019.1 | Last Updated 14 Mar 2012
Copyright © CodeProject, 1999-2014
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