views:

1770

answers:

2

I have to allow the user to enter carriage returns in text areas; something like:

Sentence 1
Sentence 2
...

I have to persist those carriage returns when loading and saving data. I use jQuery on the client side, and .NET on the server. Any suggestions on how to approach?

Thanks.

+1  A: 

You don't need to do anything special. That's what <textarea>s do, and unless you make a specific effort to strip out the newlines on the server side you'll load the save them with a standard string no problem.

John Kugelman
+2  A: 

If by "persist" the line breaks (CRLF) you mean that you want to display it properly, as SO does, you need to remeber to replace the CRLF pair with <br/>CRLF.

Otherwise, all your text will appear sequentially.

Paulo Santos
that is what I want, but found that just replacing .replace(/\r/g, '\r<br>'); is not working consistently
krul
If you're using RegEx I'd go with .replace(/\r\n/g, "\r").replace(/\r/g, "\n<br>")
Paulo Santos
If youre using php, you can take the contents of the textarea and use nl2br() to convert new lines ("\n") to <br />
lyrae
Even simpler: .replace(/\r?\n/g, "<br>\n")
the.jxc
Thanks, I forgot completely... it's the hour I think... and I guess decaf.. :-\
Paulo Santos
Thanks guys _
krul