Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apostrophe in JQuery Autocomplete Call

The code below works when searching for any name that does not contain an apostrophe. When you attempt to find someone with an apostrophe in their name it fails (returns error). How can I allow to find people with apostrophes?

function autoComplete() {
$(document).ready(function () {
    $(".AutoCompleteClass").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: "/Service/NomineeWebService.asmx/GetMatchingActiveDirectoryUsers",
                data: "{ 'SearchCharacters': '" + request.term + "' }",
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                dataFilter: function (data) { return data; },
                success: function (data) {
                    response($.map(data.d, function (item) {
                        return {
                            id: item.NomineeUserName,
                            value: item.NomineeLastNameFirstName + " - " + item.NomineeDomainAndUserName,
                            data: item
                        }
                    }))
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(textStatus);
                }
            });
        },            
        delay: 150,
        minLength: 3,            
        select: function (event, ui) {
            $('.SelectedUserNameWrapper input[type=hidden]').val(ui.item.id);
        }       
    });
});
$('#AutoCompleteTextBox').keypress(function (event) {
    if (event.which == '13') {
        alert('test');
        $('#AutoCompleteButton').click();
    }
like image 922
obautista Avatar asked Dec 13 '22 08:12

obautista


1 Answers

I found resolution to this issue, we should replace all "'" with escape characters. Code looks like below

    function autoComplete() {
$(document).ready(function () {
    $(".AutoCompleteClass").autocomplete({
        source: function (request, response) {
request.term = request.term.replace(/'/gi,"\\'"); // replace globally
like image 164
Bala Avatar answered Dec 23 '22 11:12

Bala