Here i have explained how to Limit Checkbox Selection in jQuery.

Step 1) Add HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Limit Checkbox Selection in jQuery </title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
    $(document).ready(function () {
        $('input[type=checkbox]').click(function (e) {
            var num_checked = $("input[type=checkbox]:checked").length;
            if (num_checked > 3) {
                alert("Sorry, you can't select more than 3 Checkboxes!");
                $(e.target).prop('checked', false);
            }
        });
    });
</script>
</head>

<body>
<h1> Limit Checkbox Selection in jQuery</h1>

<div> <b>Select Checkboxes</b> <small>(maximum 3 allowed)</small>   <br /><br /> </div>

<label> <input type="checkbox"> Running </label>
<label> <input type="checkbox">Boxing </label>
<label> <input type="checkbox"> Sleeping</label>
<label> <input type="checkbox"> Swimming </label>
</body>
</html>

Screenshot

Add Comments