How to restrict the amount of categories/post tags/terms for a post type post

Thanks the the replies.
I solved my problem with jQuery.

Here is the code:

jQuery(document).ready(function ($) {
$("#category-tabs li.hide-if-no-js").hide(); //Hides Most Used tab

$("#category-all input:checkbox").change(function () {
    var max = 2; // Max allowed cats
    var count = $("#category-all input:checked").length; //counts selected cats
    if (count > max) {
        $(this).prop("checked", "");
        alert('You are only allowed to select ' + max + ' categories.'); //alert message when user tries to select a third cat
    }
  });

});