JSFiddle - React, Tailwind, and code Playground

HTML

<span>Please select a maximum of two options:</span>
<select multiple id="s">
  <option>Option 1
  <option>Option 2
  <option>Option 3
  <option>Option 4
</select>
<br>
<span id="msg"></span>

JavaScript

jQuery(document).ready(function () {

    jQuery("select").on("change", function(){
      var msg = $("#msg");
    
      var count = 0;
    
      for (var i = 0; i < this.options.length; i++)
      {
        var option = this.options[i];
        
        option.selected ? count++ : null;
        
        if (count > 2)
        {
            option.selected = false;
            option.disabled = true;
            msg.html("Please select only two options.");
        }else{
            option.disabled = false;
            msg.html("");
        }
      }
    });
});