Jump to content

Copy and Paste in to .htm textarea?


Diana Watkins
Go to solution Solved by David Beagan ,

Recommended Posts

Question on textarea input within an HTML form - I have users who are attempting to copy and paste text from Word (and other places) into a textarea on an HTML form I created. As a result, this is giving varied results as (I assume) there are carriage returns, tabs, etc. included in what they are copying.  I have tried REPLACE and CTRAN to no avail.  Digging through old forums a bit, I saw where a couple of commenters stated to never EVER copy and paste text into textarea boxes from outside sources (except maybe Notepad). 

Is the general consensus that this simply won't work or has anyone been able to successfully get around it?

  • Like 1
Link to comment
Share on other sites

I had a customer who wanted to copy-and-paste a column of cells from Excel into a textarea on an HTML page that was created in HTML Canvas. This resulted in the desired data being pasted into the text area with crlf (carriage return / linefeed) characters between each value. When submitted to WebFOCUS, the crlf charaters were stripped out, making it potentially unknowable what the values were, once it gets into the WebFOCUS variable. 

My solution was to put some JavaScript on the page that would replace the crlf characters with something more useful such as commas or OR values (the JavaScript replace() method was very useful for this). If I remember correctly, I had an article published on this in one of the ibi publications. Too bad those aren't around anymore, and available for searching. 

I will check my archives and provide you with my solution later today.

Edited by David Beagan
Link to comment
Share on other sites

  • Solution

Here's an example dug up from the archives. 

I have values in Excel and have run the page. I copy-and-past a list from Excel into the textarea:

image.thumb.png.5c1afbc884548954487c28fa9e068e74.png

then when I click Go, an onblur event triggers reformats the textarea and passes the value to WebFOCUS. I just have a simple fex that does a -TYPE of the variable that was passed.

image.png.6b6504d8bfaddce85accc14cc66dcea4.png 

Here is what I did in HTML Canvas. I created on onblur event on the textarea

image.thumb.png.2685ef0e32831ed8f3410df8a35060b0.png

The following shows the JavaScript code I put in the event. The id of the textarea is just the default name that HTML Canvas gave it. Yours may be different.

image.thumb.png.54ca136dafe964bd34410f95c1d3d679.png

Here's the code for your copy-and-paste convenience: 

 document.getElementById('textarea1').value = document.getElementById('textarea1').value.replace(/\r\n/g,',');
 document.getElementById('textarea1').value = document.getElementById('textarea1').value.replace(/\r/g,',');
 document.getElementById('textarea1').value = document.getElementById('textarea1').value.replace(/\n/g,',');

The \n and \r represent carriage return and linefeed characters. The code is simplistic, for example, it puts a trailing comma at the end of the values. You could certainly do a lot more.

image.png

Edited by David Beagan
  • Like 4
  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...