Enforcing minimum number of characters for #autocomplete_path I have a form that is using #autocomplete_path on a textfield.  In general it works fine.

Is there a way to have the autocomplete callback not fire until a minimum number of characters have been entered?

Is the only real option to override Drupal.ACDB.prototype.search with my own version, or is there a clean way to do this?

I am not interested in server side solutions to limit the result set; I am trying to decrease server load here and really need a client-side solution to prevent useless requests from ever being generated.
Here's two approaches:


Replace the entire autocomplete.js with your customized version

/**
 * Implements hook_js_alter
 * Replace autocomplete.js with customized version
 */
function MY_MODULE_js_alter(&amp;$javascript) { 
  $javascript['misc/autocomplete.js']['data'] = drupal_get_path('module', 'MY_MODULE') . '/js/autocomplete.js';
}

Override Drupal.ACDB.prototype.search with a custom version


Copy to theme script.js or module js file, as long as it's added after /misc/autocomplete.js it will override it.

(function($) {

/**
 * Performs a cached and delayed search.
 *
 * Override adds minimum text string length for lookup.
 * See /misc/autocomplete.js for original function.
 */
Drupal.ACDB.prototype.search = function (searchString) {

  // Minimum search string length
  var min = 3; 
  var db = this;
  this.searchString = searchString;

  // See if this string needs to be searched for anyway.
  searchString = searchString.replace(/^\s+|\s+$/, '');
  if (searchString.length &lt;= min ||
    searchString.charAt(searchString.length - 1) == ',') {
    return;
  }

  // See if this key has been searched for before.
  if (this.cache[searchString]) {
    return this.owner.found(this.cache[searchString]);
  }
  // Initiate delayed search.
  if (this.timer) {
    clearTimeout(this.timer);
  }
  this.timer = setTimeout(function () {
    db.owner.setStatus('begin');

    // Ajax GET request for autocompletion. We use Drupal.encodePath instead of
    // encodeURIComponent to allow autocomplete search terms to contain slashes.
    $.ajax({
      type: 'GET',
      url: db.uri + '/' + Drupal.encodePath(searchString),
      dataType: 'json',
      success: function (matches) {
        if (typeof matches.status == 'undefined' || matches.status != 0) {
          db.cache[searchString] = matches;
          // Verify if these are still the matches the user wants to see.
          if (db.searchString == searchString) {
            db.owner.found(matches);
          }
          db.owner.setStatus('found');
        }
      },
      error: function (xmlhttp) {
        alert(Drupal.ajaxError(xmlhttp, db.uri));
      }
    });
  }, this.delay);
};
})(jQuery);