Typeahead Validations
How to we restrict the used to select the values only from the list?
Did you find a way to achieve this?
I don´t think that this control is designed for this per se.
You will simply have to do the validation on page level, i.e. on form submit.
You can use the events typeahead:autocompleted and typeahead:selected to grab values selected. When user selects something you can store the value and use for form validation.
In case you don´t know how (since it it is not documented in the readme.md), this is how you set-up the event handlers
$(´#yourElementIdWithHash´.on('typeahead:selected', yourSelectedEfentHanlerFunction);
And if you want both on same handler
$(´#yourElementIdWithHash´.on('typeahead:selected typeahead:autocompleted',
yourSelectedEventHanlerFunction);
And your handler functin has the signature:
function yourSelectedEventHanlerFunction(element,datum) {
//Datum contains here the info about the cell you selected
}
However what is still needed from this library is event to let you know if and when user exits the typeahead control without having current selection. For example let’s say user selects somthing in the typeahead but then decides to look for another item (by typing another query) and then exits. To test that case you must include in your validation check that the input value matches your selected datum (obtained by typeaheadÆautocompleted or typeahead:selected events)
Wow, thx so much for that, it's a really long reply
But I decided to implement it~~~ then I realize it's already in the pull request. ;D I made some final touch on that https://github.com/zhigang1992/typeahead.js
inputField.typeahead({
//url and stuff
}).on("typeahead:selected typeahead:autocompleted typeahead:matched", function (ev, datum) {
}).on('typeahead:mismatched', function() {
});
Aha, nice I’ll defiantly try this out in my fork.
The only worry is that since you are using the blur event to trigger the mismatched test that the mismatched/matched triggers will fire up each time you enter query or type new value in the query.
The typeahead control is constantly firing up blur/focus events on the input control to manage the dropdown window and the hint.
Optimal solution would be to somehow detect when you truly leave the input field and fire up match/mismatch events only then.
I didn't realize that, seem normal in my testing~ only fire when it loses focus
You are right. This blur/focus flickering may have been because of issue with browser detection for IE11 See https://github.com/twitter/typeahead.js/issues/557 The only flickering now is when you select dropdownitem by mouse click. In that case the input control is blured and refocused. That is not a real issue in this context thoug.. I think :-)