?
Solved

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

Posted on 2011-09-28
9
Medium Priority
?
2,092 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
Suggested Courses

650 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