Search code examples
jquery-tokeninput

Tokens in TokenInput (Avoiding same pattern)


I want to know how to stop inputting same pattern value in TokenInput using Jquery ?

For Example : If i have something like "GM-123" and i dont want to have "GM-345" in the input field then what should i do ?

Anyone , please help.


Solution

  • I would make use of the onAdd callback, which is called when a token is added, and then remove the token if it shouldn't be allowed. Use something like this in your set up:

    $(document).ready(function() {
        $("#search_frwId").tokenInput(getMyRestServer, {
            theme: "facebook",
            queryParam: "param",
            onAdd: function(hidden_input,item){
                 var newInput = item.name;
    
                 //Pattern Matching Magic Here
    
                 if(shouldNotBeAdded) $("#search_frwId").tokenInput("remove", item);
            }
        });
    });
    

    (This is untested code, I think passing the item back into remove like that will work, but am not 100% sure!)