Solved

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

Posted on 2011-09-28
9
1,714 Views
Last Modified: 2012-06-21
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

0
Comment
Question by:viola123
  • 5
  • 4
9 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 36720788
replace : data: "{'comm': '" + request.term +  "'}",
by : data: "{'comm': '" + encodeURIComponent(request.term) +  "'}",
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36720822
0
 
LVL 6

Author Comment

by:viola123
ID: 36735839
hi leakim971:
thanks a lot, i will have a try.
0
 
LVL 6

Author Comment

by:viola123
ID: 36741092
hi leakim971:
encodeURIComponent() will not encode: ~!*()'
how to solve those special characters: ~!*()'
the user may key in ' or () ! *.

thanks a lot
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 82

Expert Comment

by:leakim971
ID: 36747844
try this : data: "{'comm': '" + request.replace(/(\W)/g, function($1) { return "%" + $1.charCodeAt(0).toString(16); }) +  "'}",
0
 
LVL 6

Author Comment

by:viola123
ID: 36763635
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
0
 
LVL 6

Author Comment

by:viola123
ID: 36763636
am i missing reference?
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 36764690
data: "{'comm': '" + request.term.replace(/(\W)/g, function($1) { return "%" + $1.charCodeAt(0).toString(16); }) +  "'}",
0
 
LVL 6

Author Comment

by:viola123
ID: 36766058
leakim971:
you are star! it works very well.

thanks a lot
viola
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now