The following JavaScript function can be used to replace line feeds and carriage returns with commas. I find this particularly useful when formatting a data column from Excel into an SQL constraint.
Sample Input
1 2 3 4 5
Sample Result
1,2,3,4,5
The JavaScript function uses a simple regular expression to remove line feeds and carriage returns. The comma as the second argument to the ref.value.replace function may be replaced with any other character to meet your specific requirement.
Live Test
Test the code using the following text area. Enter a carriage return / line feed separated column of data, move focus off the text area, and the code will automatically format the list.
[custom_field_embedder custom_field_name="CODE_LINEFEED_REMOVER"]
Source Code
function remove_crlf(ref) { ref.value = ref.value.replace(new RegExp("[\r\n]", "gm"), ","); }
Using a simple HTML page with a Text Area, we can paste the data list into the form. When the cursor is moved away from the Text Area, the function is automatically called and the list is formatted with commas replacing the line feeds and carriage returns on each line.
Putting all of the code together, we get the following which can be saved to an html file.
<html> <head> <script language='javascript'> function remove_crlf(ref) { ref.value = ref.value.replace(new RegExp("[\r\n]", "gm"), ","); } </script> </head> <body> <form name='frmremover'> <textarea name="txtdata" rows="5" cols="40" onblur="javascript:remove_crlf(this)"></textarea> </form> </body> </html>
Thanks John, this worked perfect