• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 9685
  • Last Modified:

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

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
0
gnosticgnowledge
Asked:
gnosticgnowledge
  • 9
  • 7
  • 2
  • +5
1 Solution
 
danrosenthalCommented:
try looking for #chr(10)# and #chr(13)# which are the ascii codes for carriage return and line feed

0
 
PluckaCommented:
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
0
 
PluckaCommented:
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
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
gnosticgnowledgeAuthor Commented:
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?

0
 
gnosticgnowledgeAuthor Commented:
sorry, I keep calling it a "textbox" it is a text area.
0
 
rob_lorentzCommented:
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.
0
 
incapitalCommented:
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>
0
 
MyrandorCommented:
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.
0
 
JeffHowdenCommented:
"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.
0
 
RCorfmanCommented:
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>

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

http://cflib.org/udf.cfm?ID=419
0
 
RCorfmanCommented:
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.
0
 
JeffHowdenCommented:
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.
0
 
RCorfmanCommented:
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.
0
 
RCorfmanCommented:
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>
0
 
RCorfmanCommented:
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.
0
 
RCorfmanCommented:
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.
0
 
JeffHowdenCommented:
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).
0
 
RCorfmanCommented:
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.
0
 
JeffHowdenCommented:
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.
0
 
RCorfmanCommented:
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.
0
 
JeffHowdenCommented:
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.
0
 
RCorfmanCommented:
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>
0
 
JeffHowdenCommented:
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.
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

  • 9
  • 7
  • 2
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now