programer.tips, another cool thing from @nghuuphuoc!

Playing with Bootstrap Multiselect

Examples

Improve it on Github

Use with the Bootstrap Multiselect plugin.

The Browser option requires to choose 2-3 browsers.

<!-- Include Bootstrap Multiselect CSS, JS -->
<link rel="stylesheet" href="/vendor/bootstrap-multiselect/css/bootstrap-multiselect.css" />
<script src="/vendor/bootstrap-multiselect/js/bootstrap-multiselect.js"></script>

<form id="multiselectForm" method="post" class="form-horizontal">
    <div class="form-group">
        <label class="col-sm-3 control-label">Gender</label>
        <div class="col-sm-5">
            <select class="form-control" name="gender">
                <option value="male">Male</option>
                <option value="female">Female</option>
                <option value="other">Other</option>
            </select>
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-3 control-label">Browser</label>
        <div class="col-sm-5">
            <select class="form-control" name="browsers" multiple>
                <option value="chrome">Google Chrome</option>
                <option value="firefox">Firefox</option>
                <option value="ie">IE</option>
                <option value="safari">Safari</option>
                <option value="opera">Opera</option>
                <option value="other">Other</option>
            </select>
        </div>
    </div>

    <div class="form-group">
        <div class="col-sm-5 col-sm-offset-3">
            <button type="submit" class="btn btn-default">Validate</button>
        </div>
    </div>
</form>
$(document).ready(function() {
    $('#multiselectForm')
        .find('[name="gender"]')
            .multiselect()
            .end()
        .find('[name="browsers"]')
            .multiselect({
                // Re-validate the multiselect field when it is changed
                onChange: function(element, checked) {
                    $('#multiselectForm').bootstrapValidator('revalidateField', 'browsers');
                }
            })
            .end()
        .bootstrapValidator({
            // Exclude only disabled fields
            // The invisible fields set by Bootstrap Multiselect must be validated
            excluded: ':disabled',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                gender: {
                    validators: {
                        notEmpty: {
                            message: 'The gender is required'
                        }
                    }
                },
                browsers: {
                    validators: {
                        callback: {
                            message: 'Please choose 2-3 browsers you use for developing',
                            callback: function(value, validator) {
                                // Get the selected options
                                var options = validator.getFieldElements('browsers').val();
                                return (options != null
                                        && options.length >= 2 && options.length <= 3);
                            }
                        }
                    }
                }
            }
        });
});