views:

1402

answers:

4

We need the ability to call a handler if a user enters text without choosing from a set of autocomplete suggestions.

In our case it doesn't matter if the entered text matches any autocomplete values or not - we only care if the user clicks/selects an autocomplete value, or not.

The way our form works is that a user starts entering a contact's last-name and gets presented with autocomplete suggestions including the contact's full-name and company. If the user selects one of the autocomplete suggestions, we populate multiple fields. That's all working fine.

The new requirement is that if the user does NOT select one of the suggestions, we need to blank out multiple fields - which we'd like to implement using a handler called from an autocomplete library.

Looking at previous answers, it doesn't look like we can do this with Jörn Zaefferer's autocomplete plugin, but if you have a patch or some idea how to implement the function, this is the library we'd try and push it into.

A: 

I would think the easy way to do this would be to store the array of text suggestions and compare it with what is actually entered in the text box. Do this onblur or onsubmit. Look in the array for the actual entry...if it doesn't exist then they entered their own text.

Andrew Siemer
Two problems with that: 1 trivial - the user's allowed to deliberately ignore auto-complete if the extra meta-info is wrong - i.e. entering "Siemer" may auto-complete with "Andrew Siemer (StackOverflow)" - which would populate three fields with "Siemer", "Andrew" and "StackOverflow" - but they've ignored that option cos they're entering info for Bob Siemer at Acme Corp.Secondly, event ordering's a nightmare. onsubmit is way too late, and onblur conflicts with the way autocomplete works (onblur triggers when the user picks autocomplete values using the mouse, leading to a race-condition.)
searlea
A: 

That plugin will trigger the result event when a user selects a result. Just initialize a variable to true on page load, then set it to false in the "result" even and check to see if it is true. Something like

(function() {
  var typed_in = true;
  $("#auto").bind("result", function() { typed_in = false });
  $("#form").blur(function() {
    if (typed_in) {
      // do stuff
    } else { 
      // do other stuff
    }
    typed_in = true;
  });
)();
Paul Tarjan
I read that as suggesting using a global status variable? We'd need to use scoped/closured/expando variables to cater for multiple autocomplete fields. Bigger issue: we can't hook into `form.submit(fn)` as we're trying to help form-filling, not implement validation (i.e. we need to hook into `focus(fn)` or `blur(fn)` on individual fields as the user works their way through the form, not once they're finished)
searlea
ok, then use onblur with a closured variable. example changed
Paul Tarjan
+1  A: 

Well, here's how I did it, in case it helps. I cleared the value of a hidden field whenever the autocomplete field was left without taking a suggestion.

I believe the search() function does the actual check that you want and if result() is called without a data element, then you know that auto-complete wasn't used.

autocompleteField.result(function(event, data, formatted) {
    if (data) {
        //auto-complete matched
        //NB: this might get called twice, but that's okay
    }
    else {
        //must have been triggered by search() below
        //there was no match
    }
});

autocompleteField.blur(function(){
    autocompleteField.search(); //trigger result() on blur, even if autocomplete wasn't used
});
npdoty
this works well unless the user clicks results in the autocomplete, firing off the search too early
Luke Lowrey
A: 

jquery autocomplete have some useful functions which you can use to resolve issue. if use ignores autocomplete sugestion then u can use below code to selected values from sugestion.

suppose your autofill input name is "txtName" then you can write code as:

$('#txtName').change(function(me) { $('#txtName').search(); });
Haresh DHameliya