Note that there are some explanatory texts on larger screens.

plurals
  1. POPossible to add line breaks in a textarea field while keeping it XHTML 1.0 valid?
    text
    copied!<p><strong>EDIT:</strong> After re-reading my post I think I am being a little bit unclear about what the problem is. Let me try to re-phrase it:</p> <p>Users can leave comments on my site using a textarea field in a form. It should be possible to add line breaks in the comment using <code>&lt;br /&gt;</code>. The comment is then stored as a string in a mysql-database (escaped to make it safe) and later on retrieved from the database and displayed on the site. Regular line breaks are not recognized when the comment is displayed.</p> <p>Users can edit their comments, and in that case the original comment is displayed in a textarea field to be modified (see image below). The problem is that if there are any <code>&lt;br /&gt;</code> present, the code is not valid as XHTML 1.0 (see error message below).</p> <p>Can I make the code valid using any other type of line break? As stated in the comments, regular line breaks in combination with xml:space="preserve" does not work (line breaks are displayed in the textarea field, but not when the comment is displayed as normal text on the site).</p> <hr> <p><strong>Original question:</strong></p> <p>I have a web-form which includes a textarea field to leave comments, and I want it to be able to add line breaks using <code>&lt;br /&gt;</code>. Below is a simple example:</p> <p><a href="http://www.shipit.se/dump/textarea-dump.png" rel="nofollow noreferrer">screen dump http://www.shipit.se/dump/textarea-dump.png</a></p> <p>It works as intended, however it does not validate as XHTML 1.0 transitional when using the W3C validation service. This is the error I get:</p> <blockquote> <ol> <li>Error Line 90, Column 1587: document type does not allow element "br" here</li> </ol> <p><code>…ription" rows="0" cols="0"&gt;Test&lt;br /&gt;line break&lt;/textarea&gt;&lt;/dd&gt;&lt;dt class="cha</code></p> <p>The element named above was found in a context where it is not allowed. This could mean that you have incorrectly nested elements -- such as a "style" element in the "body" section instead of inside "head" -- or two elements that overlap (which is not allowed).</p> <p>One common cause for this error is the use of XHTML syntax in HTML documents. Due to HTML's rules of implicitly closed elements, this error can create cascading effects. For instance, using XHTML's "self-closing" tags for "meta" and "link" in the "head" section of a HTML document may cause the parser to infer the end of the "head" section and the beginning of the "body" section (where "link" and "meta" are not allowed; hence the reported error).</p> </blockquote> <p>Is there a way to make the line breaks valid code, or do I need to find a workaround (like e.g. using specific tags for line breaks in the textarea field and then replacing them with <code>&lt;br /&gt;</code> later on when displaying the comment)?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload