Reputation: 59
I am using the jQuery autocomplete to with a list of 800 values
j$(function() {
var availableTags = [item1,item2, item3,...];
j$( "#stdcodes" ).autocomplete({
source: availableTags
});
});
This works fine, what i want to do is limit or validate that the input in one the autocomplete values.
Can anyone help with this?
Upvotes: 0
Views: 4707
Reputation: 4420
Can it?
$("#my_input").autocomplete({
source: function (request, response) {
if ($.trim(request.term).length > 0) {
// THIS MY REQUEST
}
}
})
Upvotes: 0
Reputation: 7552
Try something like this:
$("#stdcodes").autocomplete({
open: function(e) {
valid = false;
},
select: function(e){
valid = true;
},
close: function(e){
if (!valid) $(this).val('');
},
source: availableTags
});
$("#stdcodes").change(function(){
if (availableTags.indexOf($(this).val()) == -1){
$(this).val("");
}
});
Added extra validation, in case autocomplete doesn't execute
Fiddle:
Upvotes: 1
Reputation: 863
Try use this way:
$("#stdcodes").autocomplete({
source: function (request, response) {
/* apply validation on request.term: */
var localResults = $.ui.autocomplete.filter(localArray, extractLast(request.term));
response(localResults);
}
});
Upvotes: 0