Validate Chosen Multi Selection with Parsley.js

2019-09-11 01:02发布

问题:

I managed to get parsleyjs.org validate a "required" Chosen dropdown but I can't seem to be able to validate the Multi selection dropdown.

I have tried to apply the max and min validation rules, that did not work.

I have setup parsleyjs.org and Choosen on a test site here: http://loai.directory/test/test/blank.php

How can I validate min and max on a multi selection dropdwon with parsleyjs?

回答1:

You should use data-parsley-mincheck for that. The following example works as expected (jsfiddle demo).

<form id="myForm">
    <select name="[]" multiple="multiple" data-parsley-required data-parsley-mincheck="2">
        <optgroup label="Section">
            <option>Drop Down Option A</option>
            <option>Drop Down Option B</option>
        </optgroup>
        <optgroup label="Section">
            <option>Drop Down Option A</option>
            <option>Drop Down Option B</option>
        </optgroup>
    </select>

    <input type="submit" />
</form>

<script>
$(document).ready(function() {
    $("select").chosen();

    $("#myForm").parsley();
});
</script>