HOW TO CHECK ANY 2 CHECKBOX FROM 4 CHECKBOXES?

jayprakash sharma

Total Post:117

Points:821
Posted by  jayprakash sharma
 1344  View(s)
Ratings:
Rate this:

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.

  1. Sumit Kesarwani

    Post:378

    Points:2694
    Re: how to Check any 2 checkBox from 4 CheckBoxes?

    Hi Jay, 


    try this:

    $("input[type='checkbox']").change(function () {

            var n = $("input:checkbox:checked").length;

            if (n > 2) {

                alert('2 are already selected!');

                $(this).attr("checked", false);

            }

        });

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!