Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 653319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:22:09+00:00 2026-05-13T22:22:09+00:00

I’m looking into using the jQuery UI autocomplete widget to implement user lookup by

  • 0

I’m looking into using the jQuery UI autocomplete widget to implement user lookup by first or last name. It looks like by default autocomplete looks up words by character sequence no matter its occurrence in a word. So if you have data such as: javascript, asp, haskell and you type in 'as' you will get all three. I would like it to only match beginning of the word. So in above example you get only 'asp'. Is there a way to configure the autocomplete widget to do this?

Ultimately it would be even better to match by beginning of first or last name like it is in Gmail.

Note: I’m trying to figure out a way to do this using the jQuery UI widget specifically. Since I’m already using jQuery UI in my project, I’m planning to stick with it and try not adding additional libraries to my web application.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T22:22:09+00:00Added an answer on May 13, 2026 at 10:22 pm

    In jQuery UI v1.8rc3, the autocomplete widget accepts a source option which can be either a string, an array, or a callback function. If it’s a string, autocomplete does a GET on that URL to get options/suggestions. If an array, autocomplete does a search, as you pointed out, for the presence of the typed chars in any position in the terms of the array. The final variant is what you want – the callback function.

    From the autocomplete documentation:

    The third variation, the callback, provides the most flexibility, and can be used to connect any data source to Autocomplete. The callback gets two arguments:

    • A request object, with a single property called "term", which refers to the value currently in the text input. For example, when the user entered "new yo" in a city field that is set to do autocomplete, the request.term will hold "new yo".
    • A response function, a callback which expects a single argument to contain the data to suggest to the user. This data should be filtered based on the provided term, and must be an array in the format allowed for simple local data: String-Array or Object-Array with label/value/both properties.

    Example code:

    var wordlist= [ "about", "above", "across", "after", "against",
                    "along", "among", "around", "at", "before", 
                    "behind", "below", "beneath", "beside", "between", 
                    "beyond", "but", "by", "despite", "down", "during", 
                    "except", "for", "from", "in", "inside", "into", 
                    "like", "near", "of", "off", "on", "onto", "out", 
                    "outside", "over", "past", "since", "through", 
                    "throughout", "till", "to", "toward", "under", 
                    "underneath", "until", "up", "upon", "with", 
                    "within", "without"] ; 
    
    $("#input1").autocomplete({
        // The source option can be an array of terms.  In this case, if
        // the typed characters appear in any position in a term, then the
        // term is included in the autocomplete list.
        // The source option can also be a function that performs the search,
        // and calls a response function with the matched entries.
        source: function(req, responseFn) {
            var re = $.ui.autocomplete.escapeRegex(req.term);
            var matcher = new RegExp( "^" + re, "i" );
            var a = $.grep( wordlist, function(item,index){
                return matcher.test(item);
            });
            responseFn( a );
        }
    });
    

    A few key points:

    • the call to $.ui.autocomplete.escapeRegex(req.term); That escapes the search term
      so that any regex-meaningful terms in the text typed by the user are treated as plain text. For example, the dot (.) is meaningful to regex. I learned of this escapeRegex function by reading the autocomplete source code.
    • the line with new Regexp(). It specifies a regexp beginning with ^ (Circumflex), which implies, it will match only when the typed characters appear at the beginning of the term in the array, which is what you wanted. It also uses the "i" option which implies a case-insensitive match.
    • the $.grep() utility just calls the provided function on each term in the provided array. The function in this case simply uses the regexp to see if the term in the array is a match for what was typed.
    • finally, the responseFn() is invoked with the result of the search.

    working demo: http://jsbin.com/ezifi

    what it looks like:

    alt text

    Just a note: I find the documentation on autocomplete to be pretty immature at this point. I didn’t find examples that did this, and I couldn’t find working doc on which .css files were necessary or which .css classes would be used. I learned all this from inspecting the code.

    See also, how can I custom-format the Autocomplete plug-in results?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 370k
  • Answers 370k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This resource is not available for development. Solution: get the… May 14, 2026 at 6:41 pm
  • Editorial Team
    Editorial Team added an answer They are not "commands", they are functions. Functions accept arguments… May 14, 2026 at 6:40 pm
  • Editorial Team
    Editorial Team added an answer Beyond the obvious answer of multiplying by 1024 (or 1000… May 14, 2026 at 6:40 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.