Jquery select2 regex plugin for valid characters?

Is there a way to define a set of valid characters for user input? E.g. alphanumeric input only. It doesn't seem to be found in the official documentation.

+3
source share
3 answers

You can accomplish this using the createSearchChoice parameter, which allows you to define a user-defined function to create tags from user input. To prevent tags containing non-alphanumeric characters, select2 should be initialized as follows:

$("#mySelect").select2({
            createSearchChoice: function(term) {
                if(term.match(/^[a-zA-Z0-9]+$/g))
                    return { id: term, text: term };
            },
            formatNoMatches: "Enter valid format text"})

, - , select2 . NoMatches, , " ", , .

Update:

select2 ~ 4.0 createTag

+3

/ , . "" , select2. - :

$('select of the select2 input or select').on('select2-open',function(e){
    // apply your mask on this element -> $('.select2-input')
});

, . Bye

0

, (select2 v3), .

select2("container") , select2("dropdown") ( ).

So my last code (for the purpose of contribution):

$('my-select2').on('select2-open',function(e){
    var input = $(this).select2("dropdown").find('.select2-input');
    // ...
});
0
source

All Articles