View Full Version : Remove line breaks from text field

03-05-2007, 07:04 PM
I've got a contact us form on a website that I use to get information from customers. I copy/paste the info they submit into a database. The problem being, that when the enter in comments in the text area, sometimes they press enter and add line breaks. What would I use as a script to automatically remove line breaks from this field so when the form is submitted, I get one long string?

Thanks in advance.

03-05-2007, 07:17 PM
I'm assuming you're using some server-side scripting since you're putting stuff in a database, so here's how to do it with php.

$message = "Hello \r\n my \n name is \r Blake";

$message = str_replace(Array("\r\n","\n","\r"), "<br>", $message);

echo $message;


Hello <br> my <br> name is <br> Blake

03-05-2007, 07:21 PM
I'm actually looking for a JavaScript I can put on my webform that will remove the line breaks from the text area field so all I have to do is copy/paste into my database. My webform is basic html on an asp page.

Thank you for your help.

03-05-2007, 07:39 PM
Ok, I assumed you were automatically inserting the text into the database, which would have required server-side scripting.

This will do it with javascript:

var message = "Hello \r\n my \n name is \r Blake";

message = message.replace(/\r\n|\r|\n/g, "<br>");