Skip to content

How to validate length of query chosen multi-select dropdown

I’m working on the validation part of the jQuery Chosen multi-select. Validation is working fine and I want to allow only one option in the multi-select dropdown. I am trying to get the length of the multi-select but it’s not working. Can anyone tell me how to do this?

Note, when I click on the ‘save’ button I want show an alert like ‘please select only one option’.

Also, multi-select has lot of options but my scenario here is if the drop-down is holding more than one option it should throw the ‘select one option only’ error, as above.

 $(".chosen-select").chosen(); 
document.getElementById("plvalidate").addEventListener("click", function(event) {
  $("#error_sp_msg_pg, #error_sp_msg_soi, #error_sp_msg_cpsea, #error_sp_msg_cpstag, #error_sp_msg_cpvari").remove();
  var soilone = document.getElementById("soilval").value;
  var cpseason = document.getElementById("seasonval").value;
  var cpstages = document.getElementById("stagval").value;

  if (soilone == "" || soilone == null) {
    //alert("Please select atleset one soil type");
    $(
      "<span/>", {
        "id": "error_sp_msg_soi",
        "html": "Please select atleset one soil type"
      }).insertBefore($("#soilval"));
    event.preventDefault();
  }

  if (cpseason == "" || cpseason == null) {
    //alert("Please select atleset one season");
    $("<span/>", {
      "id": "error_sp_msg_cpsea",
      "html": "Please select season"
    }).insertBefore($("#seasonval"));
    event.preventDefault();
  } else if (cpseason.lenght > 1) {
    alert('please select only one option');
    $("<span/>", {
      "id": "error_sp_msg_cpsea1",
      "html": "Please select only one season"
    }).insertBefore($("#seasonval"));
  }

  if (cpstages == "" || cpstages == null) {
    //alert(""Please select atleset one stage");
    $("<span/>", {
      "id": "error_sp_msg_cpstag",
      "html": "Please select atleset one stage"
    }).insertBefore($("#stagval"));
    event.preventDefault();
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.jquery.min.js"></script>

Test :
<select id="soilval" data-placeholder="Select Soil type" class="chosen-select form-control" multiple tabindex="1">
  <option>test1</option>
  <option>test2</option>
  <option>test3</option>
</select>

Test2:
<select id="seasonval" data-placeholder="Select Season" class="chosen-select form-control" multiple tabindex="1">
  <option>Test3</option>
  <option>Test4</option>
  <option>Test5</option>
</select>

Test3
<select id="stagval" data-placeholder="Select Stages" class="chosen-select form-control" multiple tabindex="1">
  <option>Test5</option>
  <option>Test6</option>
  <option>Test7</option>
</select>

<button class="btn btn-primary" id="plvalidate">Save</button>

Answer

Updated code.

$(".chosen-select").chosen();

document.getElementById("plvalidate").addEventListener("click", function(event) {
    $("#error_sp_msg_pg, #error_sp_msg_soi, #error_sp_msg_cpsea, #error_sp_msg_cpstag, #error_sp_msg_cpvari").remove();
    var soilone = $("#soilval").val();
    var cpseason = $("#seasonval").val();
    var cpstages = $("#stagval").val();
    console.log("soilone11", soilone);
    if (soilone == "" || soilone == null) {
        alert("Please select atleset one soil type");
        $(
            "<span/>", {
                "id": "error_sp_msg_soi",
                "html": "Please select atleset one soil type"
            }).insertBefore($("#soilval"));

    }
    $("#error_sp_msg_cpsea1").remove();
    if (cpseason == "" || cpseason == null) {
        //alert("Please select atleset one season");
        $("<span/>", {
            "id": "error_sp_msg_cpsea",
            "html": "Please select season"
        }).insertBefore($("#seasonval"));
        event.preventDefault();
    } else if (cpseason.length > 1) {
        alert('please select only one option');
        $("<span/>", {
            "id": "error_sp_msg_cpsea1",
            "html": "Please select only one season"
        }).insertBefore($("#seasonval"));
    }

    if (cpstages == "" || cpstages == null) {
        //alert(""Please select atleset one stage");
        $("<span/>", {
            "id": "error_sp_msg_cpstag",
            "html": "Please select atleset one stage"
        }).insertBefore($("#stagval"));
        event.preventDefault();
    }
});