Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to add line breaks in a textarea field while keeping it XHTML 1.0 valid?

EDIT: 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:

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 <br />. 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.

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 <br /> present, the code is not valid as XHTML 1.0 (see error message below).

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).


Original question:

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 <br />. Below is a simple example:

screen dump
(source: shipit.se)

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:

  1. Error Line 90, Column 1587: document type does not allow element "br" here

…ription" rows="0" cols="0">Test<br />line break</textarea></dd><dt class="cha

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).

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).

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 <br /> later on when displaying the comment)?

like image 687
aspartame Avatar asked Feb 02 '26 17:02

aspartame


2 Answers

Have you tried adding a xml:space="preserve" attribute and use just plain normal line breaks?

like image 183
Lucero Avatar answered Feb 04 '26 14:02

Lucero


Why not use regular linebreaks? Those work just fine.

If you want <br /> to appear, you need to encode those entities:

&lt;br /&gt;
like image 25
cgp Avatar answered Feb 04 '26 16:02

cgp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!