Edit in JSFiddle


              
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">

<script type="text/javascript">
  $(document).on("click", "input[type=checkbox]", function(e) {
    var num_checked = $("input[type=checkbox]:checked").length;
    if (num_checked > 3) {
      alert("sorry, you have already selected 3 checkboxes!");        
      $(e.target).attr('checked', false);
    }
  });
</script>