What character represents a new line in a text area

88

Just quick one, but want to make sure I'm catching cross platform variations.

I like to convert new lines entered into a text area into a [comma], so that the output can be represented on a single line, my question...

Currently, sending from google chrome, when I view the value, I find it uses \r\n for new lines. If I replace \r\n I know it will work for chrome on windows 7, but what about other platforms, are there variations on what other browsers will insert as a new line inside a text area?

This question is tagged with html textarea newline

~ Asked on 2013-01-08 14:27:28

The Best Answer is


106

By HTML specifications, browsers are required to canonicalize line breaks in user input to CR LF (\r\n), and I don’t think any browser gets this wrong. Reference: clause 17.13.4 Form content types in the HTML 4.01 spec.

In HTML5 drafts, the situation is more complicated, since they also deal with the processes inside a browser, not just the data that gets sent to a server-side form handler when the form is submitted. According to them (and browser practice), the textarea element value exists in three variants:

  1. the raw value as entered by the user, unnormalized; it may contain CR, LF, or CR LF pair;
  2. the internal value, called “API value”, where line breaks are normalized to LF (only);
  3. the submission value, where line breaks are normalized to CR LF pairs, as per Internet conventions.

~ Answered on 2013-01-08 14:37:48


12

Talking specifically about textareas in web forms, for all textareas, on all platforms, \r\n will work.

If you use anything else you will cause issues with cut and paste on Windows platforms.

The line breaks will be canonicalised by windows browsers when the form is submitted, but if you send the form down to the browser with \n linebreaks, you will find that the text will not copy and paste correctly between for example notepad and the textarea.

Interestingly, in spite of the Unix line end convention being \n, the standard in most text-based network protocols including HTTP, SMTP, POP3, IMAP, and so on is still \r\n. Yes, it may not make a lot of sense, but that's history and evolving standards for you!

~ Answered on 2013-01-08 14:29:24


Most Viewed Questions: