Solved

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

Posted on 2011-09-28
9
1,871 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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 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 our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

739 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