user3515428
user3515428

Reputation: 59

jQuery autocomplete validation

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

Answers (3)

ilgam
ilgam

Reputation: 4420

Can it?

$("#my_input").autocomplete({

  source: function (request, response) {

    if ($.trim(request.term).length > 0) {
      // THIS MY REQUEST
    }
  }
})

Upvotes: 0

hlscalon
hlscalon

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:

http://jsfiddle.net/Ra96R/2/

Upvotes: 1

rjdmello
rjdmello

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

Related Questions