Link to home
Start Free TrialLog in
Avatar of viola123
viola123

asked on

How to solve this issue: Autocomplete cannot accept special characters?

Hi all,
i have an autocomplete in my web page, but when i type some special characters such as \  or  '
it will popup error.

how to solve this?  the autocomplete should accept special characters because user may type any thing in the text area.

thanks a lot
<script type="text/javascript">
        $(function () {
            

            $("#tbComm").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        url: "Comments.asmx/FetchCommentary",
                        data: "{'comm': '" + 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 {
                                    value: item.Commentary
                                }
                            }))
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert(textStatus);
                        }
                    });
                }
            });
        });
*********************
<div class="ui-widget">
                        <label for="tbComm" style="text-align:left;" >Comments: 
</label><br />
                        <asp:TextBox ID="tbComm" class="field_item" runat="server" TextMode="MultiLine" 
                            Height="120px" Width="377px"></asp:TextBox>
                        
                        <br />
                        
                    </div>
*****************************
[WebMethod]
    public List<Commentary> FetchCommentary(string comm)
    {
        var comment = new Commentary();        
        var fetchComm = comment.GetCommentary()
        .Where(m => m.Commentary.ToLower().StartsWith(comm.ToLower()));
        return fetchComm.ToList();
    }

Open in new window

Avatar of leakim971
leakim971
Flag of Guadeloupe image

replace : data: "{'comm': '" + request.term +  "'}",
by : data: "{'comm': '" + encodeURIComponent(request.term) +  "'}",
Avatar of viola123
viola123

ASKER

hi leakim971:
thanks a lot, i will have a try.
hi leakim971:
encodeURIComponent() will not encode: ~!*()'
how to solve those special characters: ~!*()'
the user may key in ' or () ! *.

thanks a lot
try this : data: "{'comm': '" + request.replace(/(\W)/g, function($1) { return "%" + $1.charCodeAt(0).toString(16); }) +  "'}",
hi,
i got this error when i try request.replace(/(\W)/g, function($1) { return "%" + $1.charCodeAt(0).toString(16); })

Microsoft JScript runtime error: Object doesn't support this property or method
am i missing reference?
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
leakim971:
you are star! it works very well.

thanks a lot
viola