Textarea formatting replacing vbCrLf with <br>

When you enter text into a textarea in a HTML form and press enter for line breaks on your keyboard the line breaks are removed and not preserved when you display the entered text on a web page that receives the textarea value.

For example below we have the code for two pages "form.asp" and "form_receive.asp".

form.asp:

<html>
<body>
<form name="form1" method="post" action="form_receive.asp">
<textarea name="myTextareaBox" cols="30" rows="5"></textarea>
<br>
<input name="Submit" type="submit" value="Submit">
</form>
</body>
</html>

form_receive.asp:

<html>
<body>
<%
Dim myTextAreaVariable
myTextAreaVariable= Request.form("myTextareaBox")
Response.write myTextAreaVariable
%>
</body>
</html>

In practice the code for "form.asp" would display a textarea such as the one below. (note that the textarea below is just an example and the submit button won't work)
If I entered the following text in the textarea below and hit the submit button, the line breaks wouldn’t show up on our example page "form_receive.asp" which receives and displays the textarea value in a browser.





The page would display;

Hi,Thanks for the invite. Regards,Michael.


The simple explanation is that when you enter a carriage return into the textarea box the browser won’t pick up carriage returns. To maintain the carriage return we could simply replace the carriage return with the <br> tag using the Replace function.

To ensure proper formatting our code for "receive_form.asp" would look like this:

<html>
<body>
<%
Dim myTextAreaVariable
Request.form("myTextareaBox")
MyTextAreaVariable=Replace(Request.form("MyTextareaBox"),vbCrLf,"<br>")
Response.write myTextAreaVariable
%>
</body>
</html>

Get the best asp web hosting provider now and save 30%

Advertisements



MembersPro

MembersPro PayPal - ASP Membership software

Plug and play ASP membership script that integrates with PayPal to let you charge recurring membership fees.