Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading data from <textarea>

Status
Not open for further replies.

carlosAlberto

IS-IT--Management
Oct 9, 2001
109
0
0
GB
Hi all,

I want to read data from a form element <textarea>.

The problem that i have is, I only want to read a line at a time i.e. until the carriage return. (while ! EOL)

I'm not sure how this is done in javascript.
 
I don't think you can do that with JavaScript. There's no object I know of that you can grab for each line or for the EOL. Your best bet--if your situation allows it--is to get the textarea as it is submitted and use Perl to break apart the lines.

--Jeff
 
Code:
escape(document.formname.textareaname.value).indexOf(&quot;%0D%0A&quot;)
This will give you the index position (numerical) of the first line feed / carriage return. With some loops through the characters in the textarea value, you can get the position of every line feed / carriage return, then use the split() method to extract the lines.

It's gonna take a little time to work it out, but you can definitely do it on the client side. My question to you is, what are you intending to do with that information ??

ToddWW
 
Hi,

I have noticed if i display the string i have entered e.g.

1.1.1.1 <CR>
2.2.2.2

I get string length 16, when it should actually be 14. When i print out the characters at index's 7 and 8 it display nothing or maybe a space.

So next I searched the string for 2 spaces but found nothing. What could these two characters be????


(I would like to do this to perform some validation)
 
That's the unescaped version of the CR. You won't be able to see that.

ToddWW
 
Don't know if there is any kind of string reader object in JS that gives you a readLine() method, but you could make one up...
Code:
// StringReader constructor. Pass it a string as a parameter
function StringReader(str)
  {
  this.arr = str.split(&quot;\n&quot;); // DATA: internally uses an array of lines
  this.linesRead = 0; // DATA: tracks how many lines have been read

  // METHOD: returns the next line of text
  this.readLine = function()
    {
    if (linesRead < arr.length) return arr[linesRead++];
    else return null;
    }

  // METHOD: allows you to start over reading from first line
  this.reset = function() { linesRead = 0; }
  }
To put it to use:
Code:
// Put the textarea data into a StringReader object
var s = new StringReader(document.form.textarea.value);

// Have fun with it...
var line1 = s.readLine();
var line2 = s.readLine();
s.reset();
while (var line = s.readLine())
  {
  // do something with each line
  }
Every time you call s.readLine() you get the next line of text, without a newline character. No need to worry about the carriage return. (didn't test this. may need tweaking)

-Petey
 
Thanks for the reply Petey,

Unfortunately trying to split on the &quot;\n&quot; doesn't work. It doesn't seem to be recognising it.
 
carlosAlberto,

use wrap=&quot;virtual&quot;, it worked for me (mean splitting by &quot;\n&quot;) with this type of wrapping

here is what i used:

~~~~~~~~~

function showit(){
var formnamre, theform, txtarname, thetxtar
formname=&quot;fa&quot;
txtarname=&quot;inneed&quot;

theform=document.forms[formname]
thetxtar=theform[txtarname].value

thetxtar=thetxtar.split(&quot;\n&quot;)
for (i=0; i<thetxtar.length; i++){ alert(&quot;string number &quot;+i+&quot;: &quot;thetxtar) }

}

~~~~~~~~~ Victor
 
Vituz,

Thanks, But where does - wrap=&quot;virtual&quot; come into it??? Do i have to declare this???

I tried splitting with &quot;\n&quot; but this didn't work. I'm using ie5, is there compatibility probs somewhere with netscape??

Cheers,
 
Code:
<textarea wrap=&quot;virtual&quot;...></textarea>
ToddWW
 
damned!! tgml >:-< arghhh!!!

Code:
function showit(){
	 var formnamre, theform, txtarname, thetxtar
	 formname=&quot;fa&quot;
	 txtarname=&quot;inneed&quot;
	 
	 theform=document.forms[formname]
	 thetxtar=theform[txtarname].value
	 
	 thetxtar=thetxtar.split(&quot;\n&quot;)
		for (i=0; i<thetxtar.length; i++){ alert(&quot;string number &quot;+i+&quot;: &quot;thetxtar[i]) }
	 
	 }

:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top