Question

How do I capture carriage returns in a textbox...using Coldfusion

Asked by: gnosticgnowledge

Hi,

I am using coldfusion and have run into a little issue.  I need to capture carriage returns in a text box.  Any suggestions?  Thanks.

F

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2006-04-11 at 14:18:06ID21810443
Tags

carriage

,

coldfusion

,

return

Topics

ColdFusion Application Server

,

ColdFusion Studio

Participating Experts
7
Points
500
Comments
24

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. carriage return
    How do I force a carriage return in a multiline textbox after each entry? Each entry is a selection by means of a button.
  2. Add a Carriage Return to innerText
    Is there ANY way to add a carriage return to an innerText field that will behave the same way as if adding a <br>? I tried putting in &#13; but reporting services converts it to &amp;#13; So I need a way to add a <br> that will show up in innerText and app...
  3. Carriage Return Character
    what is Carriage Return Character.what is it advantage. how can i used Carriage Return Character in writing a file with c#.net
  4. Carriage return
    How do i write a macro that would replace the characters '\n' with a carriage return?

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: danrosenthalPosted on 2006-04-11 at 16:05:15ID: 16432249

try looking for #chr(10)# and #chr(13)# which are the ascii codes for carriage return and line feed

 

by: PluckaPosted on 2006-04-11 at 16:52:33ID: 16432464

gnosticgnowledge,

If you are refering to capturing them as they are entered, then you need to use javascript to do this.

<script language="javascript">
    document.onkeydown = KeyPress;

    function KeyPress() {
        <!--- Capture Return --->
        if (event.keyCode == 13 && event.srcElement.type != 'textarea') {
            alert('Return Pressed');
        }
    }
</script>
Regards
Plucka

 

by: PluckaPosted on 2006-04-11 at 16:53:23ID: 16432468

gnosticgnowledge,

Oops, that does it for non text areas, this will just do text areas

<script language="javascript">
    document.onkeydown = KeyPress;

    function KeyPress() {
        <!--- Capture Return --->
        if (event.keyCode == 13 && event.srcElement.type == 'textarea') {
            alert('Return Pressed');
        }
    }
</script>

Regards
Plucka

 

by: gnosticgnowledgePosted on 2006-04-12 at 07:31:46ID: 16436482

Hmmm. Perhaps my question isn't clear.

If a users is entering information into a textbox and they want to press "Enter" to seperate the lines, how can I carry this "enter" across in the form variable?

 

by: gnosticgnowledgePosted on 2006-04-12 at 07:45:11ID: 16436608

sorry, I keep calling it a "textbox" it is a text area.

 

by: rob_lorentzPosted on 2006-04-12 at 08:51:59ID: 16437443

I have handled this by converting all the chr(10)chr(13) to <br>. This way anywhere you display the users input it will be displayed correctly. If you allow the users to edit the data input you will need to convert the <br> back to chr(10)chr(13) so it will be formatted correctly in the textarea.

I can dig up some examples if you need.

 

by: incapitalPosted on 2006-04-12 at 09:45:08ID: 16438033

The carraige returns should be captured without any additional effort on your part. The trick is rendering them as HTML. There are several UDFs and custom tags that will convert the carraige returns to <br> or <p>.

This UDF should do the trick:

<cfscript>
      function textAreaFormat(theString)
      {
            var workingString = "";
            var outputString = "";
            
            workingString = theString;
            workingString = replace(workingString,chr(13)&chr(10),"<br>","all");
            workingString = replace(workingString,chr(32),"&nbsp;","all");
            outputString = workingString;
            return outputString;
      }
</cfscript>

 

by: MyrandorPosted on 2006-04-12 at 14:58:12ID: 16440974

The key is to use the "WRAP=HARD" to keep the carriage return.
Example: <TEXTAREA NAME="name" WRAP=HARD>

After that, you can use chr() detection to force a replacement to <br> to output it in HTML, or just put the content in a new TEXTAREA and the carriage return should be keep.

 

by: JeffHowdenPosted on 2006-04-12 at 20:52:22ID: 16442581

"WRAP=HARD" is unnecessary and invalid HTML.  Carriage returns, tabs, any every other character entered into a textarea is sent when the form is posted.  The trick, as has been stated several times, is converting the associated bits to HTML that'll result in the string being rendered similarly on a webpage.

 

by: RCorfmanPosted on 2006-04-15 at 23:44:16ID: 16463032

The ParagraphFormat function does some of the formatting...ParagraphFormat it seperates double-sets of crlf with <p> tag, but not single sets.

As already stated, when the form variable is passed back to the action from, the data is in the passed variable.

I process this data like this... seems a little messy, but it keeps the single carraiage return and multiple carriage return lines all straight and equivalent to the way the user entered it, and it takes care of any other special characters, like ", <, etc that the user entered...

In my case, the variable that contained the data is named comments, and I also create a variable named... CRLF prior...
<CFSET CRLF="#Chr(13)##Chr(10)#">
<cfset linebreak="">
<cfloop index="paragraph" list="#replace(comments,"#CRLF##CRLF#","#CRLF# #CRLF#")#" delimiters="#CRLF#">
     #linebreak##HTMLEditFormat(paragraph)#<cfset linebreak="<br>">
</cfloop>

 

by: JeffHowdenPosted on 2006-04-16 at 11:32:53ID: 16465231

Unfortunately,  RCcorfman, that's a mess of non-semantic HTML.  Instead use a UDF:

http://cflib.org/udf.cfm?ID=419

 

by: RCorfmanPosted on 2006-04-16 at 11:44:36ID: 16465283

I'm not sure what you are meaning... I use this today and it outputs the data perfectly on an html page when the data was entered in a text block of a form.

 

by: JeffHowdenPosted on 2006-04-16 at 11:50:27ID: 16465306

Yes, I'm sure the visual output works well.  However, text with a bunch of <br> tags embedded in it doesn't have any semantic significance compared to the same text with blocks wrapped in <p> tags.

 

by: RCorfmanPosted on 2006-04-16 at 12:03:52ID: 16465360

It does if you have single CR sections vs. Multiple CR return sections.  A single CRLF is equivalent to a BR.  Pairs of them are the same as <P> tags... but what about sets like 3, or 5... <p> tags don't work as well as recognizing each CRLF combination and replacing it with a <br>... Also, the replace I did puts a space in-between the pairs of CRLF combinations so that the cfloop with the list delimeter recognizes them. I'll put together a little standalone page that demonstrates this in action.

 

by: RCorfmanPosted on 2006-04-16 at 12:24:12ID: 16465440

Save the following as a .cfm file named textBlockTest.cfm then play with it to see how the input block of text is replicated back out below it, and re-initialized back into the block. Handles all special characters.

<!--- form to test the data input/output with--->
Enter text in the block, including Carriage Returns and special Characters.<br>
It will be displayed back correctly in both the text block and the output below it.
<FORM ACTION="textBlockTest.cfm" METHOD="post">
<TEXTAREA NAME="testTextArea" cols="40" rows="5" wrap="PHYSICAL">
<!--- output the initial text in the input block if it is provided --->
<cfif isdefined("testTextArea")><cfoutput>#testTextArea#</cfoutput></cfif></TEXTAREA>
<br><br>
<input type="submit" value="Submit">
<hr>
<!--- if the variable is defined, then output it below the text area too --->
<cfif isdefined("testTextArea")>
<cfoutput>
<!--- define CRLF variable for easier reading --->
<CFSET CRLF="#Chr(13)##Chr(10)#">
<!--- initialize the line break to spaces for the first pass through the data --->
<cfset linebreak="">
<!--- now, loop through the input data, replacing all CRLF combinations with <br> tags --->
<cfloop index="paragraph" list="#replace(testTextArea,"#CRLF#","#CRLF# ","ALL")#" delimiters="#CRLF#">
     #linebreak##HTMLEditFormat(paragraph)#<cfset linebreak="<br>">
</cfloop>
<!--- also output the data in urlEncoded format just for kicks --->
<hr>#urlEncodedFormat(testTextArea)#
</cfoutput>
<hr>
</cfif>

 

by: RCorfmanPosted on 2006-04-16 at 12:25:07ID: 16465443

Please NOTE, I changed the output. Turns out I had a problem with the replace in not handling ALL CRLF combinations in the output. My users hadn't reported this bug to me in my production code... The loop output in the sample above does work properly.

 

by: RCorfmanPosted on 2006-04-16 at 12:28:33ID: 16465454

I will also note, in fairness, this is very similar to incapital's solution, but they didn't accomodate the other special characters, like ", < and >.  I like the idea of a function though, that makes sense if you have multiple cases of it and are using MX6 or higher... for 5 and below, the code I show will work on virtually any version, but it isn't as clean as a nice function is.

 

by: JeffHowdenPosted on 2006-04-16 at 12:30:16ID: 16465458

The UDF I linked to handles double and single crlf sequences.  It also properly *wraps* paragraphs with <p> and </p> blocks instead of just using a <p> tag (which isn't valid XHTML).

 

by: RCorfmanPosted on 2006-04-16 at 12:46:58ID: 16465525

I'm not using <p> tags at all... and it looks like it won't properly output if there passed in string has the other special characters like " < >, etc.  It doesn't convert them, and once you embed the <p></p> blocks, then you can't use htmlEditFormat. Maybe I don't understand, I'm just claiming that what I provided works in all cases, not that someone elses solution doesn't work, just that I "think" there may be other issues in the other provided solutions.  I'm not vouching that they do or don't work though, just that what I'm showing does work.  It is also my understanding that using <br> tags is OK, but I don't claim to be an XHTML expert, or even an html expert, just someone who uses cold fusion and tries to get the users data to output correctly.

 

by: JeffHowdenPosted on 2006-04-16 at 14:18:15ID: 16465860

Well, in my use, I don't permit < and > in the source, so replacing it with HTML character entities isn't really relevant or necessary.  While technically " should be replaced, it's not a requirement for proper display.

Anyway, I keep posting about properly marking up the content with HTML because I believe there's value in doing it right and that using <br> only gets you halfway there.

 

by: RCorfmanPosted on 2006-04-16 at 14:25:30ID: 16465888

so, when I use <p>text</p><p>text</p>, I get a blank line between the two... How do you force a CR at the right place without using <br> in html. If there is a better way, I'm open to it.

 

by: JeffHowdenPosted on 2006-04-16 at 15:30:45ID: 16466095

You're asking a question that really has a lot more background to the answer than can be covered in a single question.  I'll give it a try with a simple example.

Well, Let's say you had the following:

Phone: (800) 555-1212
Email: foo@example.com
Website: http://example.com/
Address: 1234 Any St.,
Any City, NY  10101

It might be tempting to mark that bit up in the following manner:

<strong>Phone:</strong> (800) 555-1212<br>
<strong>Email:</strong> <a href="mailto:foo@example.com">foo@example.com</a><br>
<strong>Website:</strong> <a href="http://example.com/">http://example.com/</a><br>
<strong>Address:</strong> 1234 Any St.,<br>
Any City, NY 10101<br>

However, while that markup visually improves the text, it doesn't give any indication what the text actually is.  Take the following instead:

<dl>
  <dt>Phone:</dt><dd>(800) 555-1212</dd>
  <dt>Email:</dt><dd><a href="mailto:foo@example.com">foo@example.com</a></dd>
  <dt>Website:</dt><dd><a href="http://example.com/">http://example.com/</a></dd>
  <dt>Address:</dt><dd><address>1234 Any St.,<br />Any City, NY 10101</address></dd>
</dl>

Another example:

We need the following items:
1. Shoes
2. Pants
3. Shirts
4. Socks
5. Hat
6. Gloves

Marked up as you suggest, it becomes:

1. Shoes<br>
2. Pants<br>
3. Shirts<br>
4. Socks<br>
5. Hat<br>
6. Gloves<br>

However, it's really far more appropriate as an ordered list:

<ol>
  <li>Shoes</li>
  <li>Pants</li>
  <li>Shirts</li>
  <li>Socks</li>
  <li>Hat</li>
  <li>Gloves</li>
</ol>

Some of these things are easier to accomplish with a function that does alot of Replace() function calls than others are.  In all things though, there's value in trying to markup the content in as descriptive manner as possible.

 

by: RCorfmanPosted on 2006-04-16 at 15:43:43ID: 16466130

In this case, I would assert that <br> is correct then. What we are trying to do is display back the input by a user in a text box... there is no other markup to convey meaning than <br>. What I didn't know is if there is a better way in this case, or if <br> is a "bad" tag. Sounds like not. In fact, replacing a CRLF with a <p></p> block CHANGES what the input box had, it inserts an extra blank line that the user did not input.  Based on what you are saying, I stand by saying that <br> is the proper tag <u>in this case</u>

 

by: JeffHowdenPosted on 2006-04-16 at 22:56:58ID: 16467236

If it's simply echoing back input from a user, than <br> is fine.  However, if that input is going to be used for more permanent display (ie, the user is typing in content for a portion of a page on a site via an admin panel), then a more semantic approach would be most appropriate.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...