newlines in textarea on different platforms

J

Jos van Uden

Hello,

Recently I created a validation script for a form
that checks a textarea. The visitor is invited to
send a list of 25 words, with each word on a new
line.

Before I submit the list I run a test that checks
the length of each word. In order to do this I split
the content of the textarea to create an array of
words that I can loop through.

For the split I'm using the following code:

var entries = list.value.split("\n");

It works fine with me, but I'm wondering if this
will also work on Mac computers.

Or should I write something like:

if(navigator.appVersion.indexOf("Mac") != -1) {
var entries = list.value.split("\r");
} else {
var entries = list.value.split("\n");
}



Thanks,

Jos

(PS. I know that windows uses "\r\n" but that is
dealt with in the subsequent code).
 
A

ASM

Jos said:
Hello,

Recently I created a validation script for a form
that checks a textarea. The visitor is invited to
send a list of 25 words, with each word on a new
line.

Before I submit the list I run a test that checks
the length of each word. In order to do this I split
the content of the textarea to create an array of
words that I can loop through.

For the split I'm using the following code:

var entries = list.value.split("\n");

It works fine with me, but I'm wondering if this
will also work on Mac computers.


yes '\n' works fine on (my) Mac

<form>
<textarea name="vue" cols=30 rows=10></textarea>
<a href="#"
onclick="alert(document.forms[0].vue.value.split('\n'));">voir</a>
</form>
 
F

Fred Oz

ASM said:
Jos van Uden wrote:
[...]

yes '\n' works fine on (my) Mac

<form>
<textarea name="vue" cols=30 rows=10></textarea>
<a href="#"
onclick="alert(document.forms[0].vue.value.split('\n'));">voir</a>
</form>

Here's an alternative, provided a 'word' is delimited by any amount of
white space (including returns, newlines, tabs, whatever). I can't
think of any words that have whitespace in them, but I don't know many
languages!

<form action="">
<textarea name="ta"></textarea>
<input type="button" value="click me" onclick="
var x = this.form.ta.value.split(/\s+/);
alert(x.join('\n'));
">
 
M

Mick White

Jos said:
Hello,

Recently I created a validation script for a form
that checks a textarea. The visitor is invited to
send a list of 25 words, with each word on a new
line.

Before I submit the list I run a test that checks
the length of each word. In order to do this I split
the content of the textarea to create an array of
words that I can loop through.

For the split I'm using the following code:

var entries = list.value.split("\n");

var entries = list.value.split(/[\r\n]/);

Mick
 
V

VK

For the split I'm using the following code:
var entries = list.value.split("\n");
It works fine with me, but I'm wondering if this
will also work on Mac computers.

It works everywhere *within JavaScript*. Its "\n" sequence became kind
of getSystemDelimiter() from Java. So if you do:
myTextArea.value = string1 + "\n" +string2;
everyone will be happy:
Mac users will get "\r", Linux users will get "\n", Win users will get
"\r\n";

But if your are dealing with some text generated outside of JavaScript,
then of course "\r\n" or "\r" or "\n" are very different. Mick White
gave you an universal split().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top