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 dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

textarea format 1

Status
Not open for further replies.

jriggs420

Programmer
Sep 30, 2005
116
US
How can I check for blank lines in a textarea, and remove them? I am thinking that I will need to populate an array with the data from the textarea first. Ideally, I would like to be able to perform this before the data is submitted and do it automatically. Also, I would like to be able to alphabetize the items in the textarea as well. I'm not entirely sure how to go about this in js. Any suggestions? TIA-
Joe

A clever person solves a problem.
A wise person avoids it.

-- Einstein
 
example:

Code:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
<script type="text/javascript"><!--
function doThing(t) {
    var v = t.value.replace(/\r\n|\n|\r/g, " ");
    t.value = v;
}
//--></script>
</head>

<body>
<form>
<textarea name="t1" onblur="doThing(this);"></textarea>
</form>
</body>

</html>



*cLFlaVA
----------------------------
[tt]I already made like infinity of those at scout camp...[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
Thanks cL,
Your solution works but it isn't exactly what I had in mind. Looks to me like it simply removes all the newline chars. I only want to remove those that are in front of a blank line. If I was coding in perl, I'd use the expression
Code:
/^\s*$
and to remove a blank line,
s/^\s*$//g;
where '/s' is for lack of a better word a space. I've already tried to plug this into the code you've given me, but I'm guessing windoze uses something different to denote a 'space'. But thank you nonetheless you've given me enough so that I can figure out the rest w/ google.

A clever person solves a problem.
A wise person avoids it.

-- Einstein
 
Your solution works but it isn't exactly what I had in mind. Looks to me like it simply removes all the newline chars. I only want to remove those that are in front of a blank line.

no offense, but how did you expect anybody to magically realize that's what you were after, based on your original question?



*cLFlaVA
----------------------------
[tt]I already made like infinity of those at scout camp...[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
Hmm, now that I re-read it, you are right. I forget that you guys can't see it like I can. I suppose I should've posted some sample data.
__INDAT____
heres the stuff that I want to keep
since the next line is blank, I don't want it

some more stuff after
the last line could be blank too.

<--eof

__OUTDAT__
heres the stuff that I want to keep
since the next line is blank, I don't want it
some more stuff after
the last line could be blank too.
<--eof
I don't know if it's important, the text is contained within a textarea, and copy/pasted from a word doc. I consider a blank line to be a line that doesn't contain letters, only (possibly) formatters e.g. \s\r\t etc between the newline chars.. Hope that makes a little more sense.

A clever person solves a problem.
A wise person avoids it.

-- Einstein
 
jriggs420, there might be a faster way to do this, but this works for me, it's 3 steps:
Code:
<html>
<head>
<script language="javascript" type="text/javascript">

function blah(str) {
   //get rid of carriage returns so we only have to work with line feeds
   str = str.replace(/\r/g, "");
   //remove all trailing spaces from each line
   while (/\s\n/.test(str)) {
      str = str.replace(/\s\n/g, "\n");
   }
   //remove all consecutive newline characters
   while (/\n\n/.test(str)) {
      str = str.replace(/\n\n/g, "\n");
   }
   alert(str);
}

</script>
</head>
<body>
<textarea id="ta" style="height:200px; width:300px"></textarea>
<input type="button" value="click me" onclick="blah(document.getElementById('ta').value)">
</body>
</html>

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
 
Grab the textarea value.
Use split() to populate an array with the textarea's value splitting on the carriage return/line feeds.
Use sort() to alphabetize the array.
Loop through the contents of the array writing the data back to the textarea or to a hidden form field skipping any array entries that contain no data. Append whatever characters you need for carriage returns (if you need them) for your processing page.




Stamp out, eliminate and abolish redundancy!
 
This works with strict constraint on eliminating the bogus ending lines if exist.
[tt]
function formatit(obj) {
var s=obj.value;
/* either this
s=s.replace(/(\r(\s*\r)+)/g,"\r");
s=s.replace(/\r\n$/,""); //this is strict
*/
//or this [II]
s=s.replace(/(\n(\s*\n)+)/g,"\n");
s=s.replace(/\r\n$/,""); //this is strict
objtxt.value=s;
}
[/tt]
The textarea calls onblur="formatit(this)".

 
This takes the current value, stores it into an array, sorts the array, then builds an output string eliminating any empty lines or lines that only contain spaces.

Code:
<html>
<head>
<title></title>
<SCRIPT LANGUAGE="javascript">
function processtext()
{
  var myout = '';
  var mytxt = document.getElementById('mytxtin').value;
  var arrmytxt=mytxt.split('\r\n');
  arrmytxt.sort();
  for (var x=0;x<arrmytxt.length;x++)
  {
    if (not_empty(arrmytxt[x]))
      myout = myout + arrmytxt[x] + '\r\n';
  }
  document.getElementById('mytxtout').value = myout;
}

function not_empty(value)
{ //Strips leading and trailing whitespace and tests if anything remains.
  var re = (value.replace(/^\s+|\s+$/g,'').length > 0)?true:false;
  return re;
}
</SCRIPT>
</head>
<body>
<form>
  <textarea id="mytxtin" cols="60" rows="6"></textarea><br><br>
  <textarea id="mytxtout" cols="60" rows="6"></textarea><br>
  <input type="button" value="Go" onclick="processtext()">
</form>
</body>
</html>


Stamp out, eliminate and abolish redundancy!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top