Solved

Coldfusion, Jquery and special characters

Posted on 2012-04-09
7
376 Views
Last Modified: 2012-04-14
I am using jquery to populate a form.   Here is the way that I am doing it:

<script type="text/javascript">
      $(document).ready(function(){
            <cfoutput query="qry_sel_info">
                  $("##notes").val("#notes#");
            </cfoutput>
       });
</script>

The problem I'm running into is special characters.  Users are entering single quotes (which in this case works), double quotes, and hitting <enter> after lines.  How can I fix this so it will show all the special characters without having problems.  

I'm not too good at regular expressions, but I gave it a shot and here is what I've tried so far:

<cfset aNote = #REReplace(admin_note,"([\'\""])","\\\1", "all")#>
$("##notes").val("#anotes#");

This works to a point.  The problem is that in my textarea it shows:
&quot;This is a 'test'&quot;

The single quotes work the way I want, but I don't want &quot;, i actually want the " to show.  Plus I have no idea what to do with a <enter>

Another issue is that I have a TON of fields that this effects.  What is the easiest way to do this without having to use cfset on every field?  I would assume a function...but i'm fairly new to jquery.  

Thanks in advance for your help
0
Comment
Question by:trifecta2k
  • 4
  • 3
7 Comments
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
(no points ..)

I haven't tested it, but I normally use jsStringFormat which properly escapes input for use in javascript.


Plus I have no idea what to do with a <enter>
Depends on the field type.  Most html elements (except maybe textarea) can't display ascii new lines ie chr(10). If you want to preserve/display them, convert them to html line breaks instead ie <br>.  

So maybe something like    

            $("##notes").val( '#jsStringFormat( replace(notes, chr(10), "<br>", "all") )#' );

Another issue is that I have a TON of fields that this effects.

Then you might consider creating a wrapper function, and call it instead.
0
 
LVL 5

Accepted Solution

by:
trifecta2k earned 0 total points
Comment Utility
Looks like jsStringFormat works by itself.  I don't need the chr(10) stuff.  jsStringFormat already escapes newline.  I guess that's all I was looking for.  Thanks!
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
Ok, kewl. I couldn't remember off the top of my head :)
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 5

Author Comment

by:trifecta2k
Comment Utility
agx..you wrote no points...how do I accept this as a solution without giving you points?
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
I think you can accept one of your own comments as an answer.
0
 
LVL 5

Author Comment

by:trifecta2k
Comment Utility
OK.  I will do that.  Thanks again for all of your help.
0
 
LVL 5

Author Closing Comment

by:trifecta2k
Comment Utility
agx didn't want points so I selected my own answer as the solution.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
This article discusses how to create an extensible mechanism for linked drop downs.
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…

728 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

12 Experts available now in Live!

Get 1:1 Help Now