user2298581
user2298581

Reputation: 682

javascript paste text into a Textarea remove (CR) from CRLF

I need to preserve the line breaks of an text input inserted in my javascript front-end page, after being pasted into a Textarea input

this is what happens if I debug the value contained in the onSumbit() method of the textarea notepad++ decoded text

I need to have CRLF but the image clearly shows that the CarriageReturn (CR) have been removed from the text inserted and only LineFeed (LF) is still present.

I've tried to add the css property white-space: pre-wrap to the textarea but that didn't work.

How can I preserve the CRLF of the text?

thanks

Upvotes: 0

Views: 252

Answers (1)

Dimitrios Tsalkakis
Dimitrios Tsalkakis

Reputation: 1102

Where are you trying to encode the text to base64? If you are doing it in the frontend using Javascript to take the value, you should know that the value contains only LF. If you are doing it in the backend after a body submit by POST then yes, the value contains CRLF and something is wrong.

But my guess from your issue description is that you are in the first -frontend- case, so it's not the encoding to blame.

So, when you are getting the value from the textarea, you can do something like

value.replace(/\r\n/g, "\n").replace(/\n/g, "\r\n")

to always get CRLF.

Upvotes: 1

Related Questions