Click to See Complete Forum and Search --> : textarea to string conversion


ZooTek
09-29-2003, 05:14 PM
I haven't written any javascript in so long that I can not remember how to go about this. I need a function which will do 3 things and then I'm pretty sure I can manage the rest of it.

1) read the text from a form textarea.
var uci = document.list.txtarea.value

2) seperate the text each time a comma appears, is this the correct way to prepare for that and if so how do I implement it?
char = unescape("%2C");

3) Take each of the seperated pieces of text and create an array from them.

Also, I plan to code this to sort the values of the array alphabetically before the final write function is called (this is a text-to-javascript conversion script). Should I perform that function before the text it placed into the array or after?

Thanks for any assistance with this ~ ZT

pyro
09-29-2003, 06:05 PM
Try something along these lines:

<script type="text/javascript">
function convert() {
uci = document.myform.mytext.value;
ary = uci.split(",");
ary.sort();
for (i in ary) {
alert (ary[i]);
}
}
</script>
</head>
<body>
<form action="" name="myform">
<textarea name="mytext" cols="50" rows="5"></textarea>
<input type="button" onclick="convert();" value="Convert">
</form>

ZooTek
09-29-2003, 09:56 PM
Thank you Pyro, that's a great solution, I had never seen the use of "in" before. I have posted the final working code below. One last question, is there any way to tell the script to ignore spaces directly following the comma(s) in the textarea? If not, can I disallow spaces from being typed into the textarea and have the script convert underscores to spaces in the output?


<script language="javascript">
function getList() {
rNum = document.inform.rNum.value;
uci = document.inform.input.value;
ary = uci.split(",");
ary.sort();
var i, len = ary.length
for (i in ary) {
code = 'rcrd['+rNum+'] = "'+(ary[0])+'"';
}
for (i = 1; i < len; i++) {
code += '\r+ "\\r'+(ary[i])+'"';
}
code += ';';
document.outform.output.value = code;
}
</script>
</head>

<body>
<div align='center'>
<form action="" name="inform">
Record Number: <input type="text" size="2" name="rNum"><br>
<textarea name="input" cols="50" rows="5"></textarea>
<br>
<input type="button" onclick="getList();" value="Convert">
</form>
<form action="" name="outform">
<textarea name="output" cols="50" rows="5"></textarea>
</form>
</div>

pyro
09-29-2003, 10:09 PM
Swap out the old function I gave you for this new one:

function convert() {
uci = document.myform.mytext.value;
uci = uci.replace("/,\s/g", ",");
ary = uci.split(",");
ary.sort();
for (i in ary) {
alert (ary[i]);
}
}What it will do is replace all the commas followed by a white space with just a comma.

ZooTek
09-29-2003, 11:10 PM
Ok the only line that I needed to add was
uci = uci.replace("/,\s/g", ",");
correct? because it does not remove the spaces trailing commas in my script :(

ZooTek
09-30-2003, 12:15 AM
nevermind, I figured it out, you left out the i after the g and the reg expression doesn't need to be in quotes. Thanks for gettin gme on the right track though!

uci = uci.replace(/,\s/gi,",");

pyro
09-30-2003, 07:33 AM
Ah, sorry about that... Yes, the regexp can't be inside quotes (when done like that, anyway). The i at the end, however, isn't necessary, as that is just the case-insensative flag, and since we are only looking for commas and spaces, it's not necessary.

ZooTek
09-30-2003, 01:39 PM
Thanks for clearing that up, the site I used to figure it out didn't say why the i was used. I do have one more question for you though. Is there a way to say in the regexp to ignore any quantity of spaces following the comma, rather than just one space? Also, can it be written to ignore any spaces that occur between the last letter of text and the following comma?
i.e.. change "hello ," to "hello,"
I only want spaces to appear between words, so that the end product looks like
"hi there,hello world,goodbye cruel world"

pyro
09-30-2003, 10:45 PM
Yep:

uci = uci.replace("/,\s*/g", ",");

The * means 0 or more of the previous character...

ZooTek
10-01-2003, 02:48 AM
ok so to get it to do everything I want it to (no spaces after comma & no spaces before comma), I wrote it like this:

uci = uci.replace(/,\s*/g, ",");
uci = uci.replace(/\s*,/g, ",");

Is there a shorter way to write this piece of code?

pyro
10-01-2003, 07:04 AM
Yes, like this:

uci = uci.replace(/\s*,\s*/g, ",");

ZooTek
10-01-2003, 06:33 PM
Perfect, thanks!

pyro
10-01-2003, 06:35 PM
You're welcome... :)