Twitter bootstrap typeahead - how to only allow values ​​from a list?

Is there a way to allow the user to select values ​​from a predefined list only using the bootstrap type Twitter? Ideally, each text value should be replaced with some numerical value (as is possible with select ).

For instance:

 var regionsList = ["value1", "value2", "value3"]; var regionsCodesList = [11, 12, 77]; $('#region').typeahead({ source: regionsList }); 

The user should be able to enter only value1, value2 or value3 and nothing more. After submitting the form, values ​​such as 11, 12, 77 should be passed accordingly.

Upd. I created a special verification method:

 $.validator.addMethod("validRegion", function (value, element, param) { return !(param.indexOf(value) == -1); }, "Please start to input the region and then choose the value from the list"); 

regionsList is passed as param . But this will not work - if the user selects a value from the list with the mouse, then the error message is not hidden.

+4
source share
1 answer

Try the update method:

 $('#region').typeahead({ // .... "updater" : function bla(item) { $('#region').val(item); $("#myForm").validate().element("#region"); return item; } } } 
0
source

All Articles