Solved

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

Posted on 2011-09-28
9
1,814 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The viewer will learn how to dynamically set the form action using jQuery.
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…

828 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