How to ignore only whitespace in select2.js

In the input field, when I only type in spaces, then it is accepted and displayed blank with a cross icon. Please provide me with a solution. How to deal with this.

My code:

$("#userfrm #field_value").select2({
        maximumInputLength: 20,
        tags:tags,
        maximumSelectionSize : 10,
        createSearchChoice:function(term, data) { if ($(data).filter(function() { return this.text.localeCompare(term)===0; }).length===0) {return {id:term, text:term};} },
        multiple: true,
    });

      

+3


source to share


1 answer


You can listen to the select2-selecting

event to reject empty input:

$("#userfrm #field_value").select2({
    maximumInputLength: 20,
    tags: tags,
    maximumSelectionSize: 10,
    createSearchChoice: function(term, data) {
        if ($(data).filter(function() {
            return this.text.localeCompare(term) === 0;
        }).length === 0) {
            return {
                id: term,
                text: term
            };
        }
    },
    multiple: true,
})
.on('select2-selecting', function(e) {
    if (!$.trim(e.val)) {
        e.preventDefault();
    }
});

      



Demo: http://plnkr.co/edit/3Z64ZCBiIu8HU5hnAFI7?p=preview

+3


source







All Articles