textarea NewLine Value

G

Gene Wirchenko

Dear HTMLers:

Suppose I have a textarea control on a page. The user enters a
multi-line comment in it. I need to be able to do some clean up on
the entered value on the frontend, and I want to know what the newline
character(s) is. What can I count on for the value of the newline
character(s)?

I am using IE 9 under Windows 7, and a bit to my surprise, the
newline is LF. I do not want to write browser-specific code though.

I found "It is recommended that user agents canonicalize line
endings to CR, LF (ASCII decimal 13, 10) when submitting the field's
contents. The character set for submitted data should be ISO Latin-1,
unless the server has previously indicated that it can support
alternative character sets." at
http://www.w3.org/TR/REC-html32#textarea, but this does not say
anything about the value before submission.

Sincerely,

Gene Wirchenko
 
D

Denis McMahon

Dear HTMLers:
Suppose I have a textarea control on a page. The user enters a
multi-line comment in it. I need to be able to do some clean up on the
entered value on the frontend, and I want to know what the newline
character(s) is. What can I count on for the value of the newline
character(s)?

It's not really an html question, more a javascript one. ;)

If you're asking how to clean it up in javascript, one approach that
might work is:

document.getElementById("id").value = document.getElementById
("id").value.replace(/[\r\n]+/g,"\r\n");

This will replace any sequence of 1 or more of any combination of
carriage returns and line feeds with a single carriage return / linefeed
pair in the form element identified by the given id.

Rgds

Denis McMahon
 
A

Adrienne Boswell

Gene Wirchenko said:
Dear HTMLers:

Suppose I have a textarea control on a page. The user enters a
multi-line comment in it. I need to be able to do some clean up on
the entered value on the frontend, and I want to know what the newline
character(s) is. What can I count on for the value of the newline
character(s)?

I am using IE 9 under Windows 7, and a bit to my surprise, the
newline is LF. I do not want to write browser-specific code though.

I found "It is recommended that user agents canonicalize line
endings to CR, LF (ASCII decimal 13, 10) when submitting the field's
contents. The character set for submitted data should be ISO Latin-1,
unless the server has previously indicated that it can support
alternative character sets." at
http://www.w3.org/TR/REC-html32#textarea, but this does not say
anything about the value before submission.

Your best bet is to do it server side. What you propose won't work if
the visitor has JavaScript disabled.
 
N

Neil Gould

Gene said:
Dear HTMLers:

Suppose I have a textarea control on a page. The user enters a
multi-line comment in it. I need to be able to do some clean up on
the entered value on the frontend, and I want to know what the newline
character(s) is. What can I count on for the value of the newline
character(s)?

I am using IE 9 under Windows 7, and a bit to my surprise, the
newline is LF. I do not want to write browser-specific code though.

I found "It is recommended that user agents canonicalize line
endings to CR, LF (ASCII decimal 13, 10) when submitting the field's
contents. The character set for submitted data should be ISO Latin-1,
unless the server has previously indicated that it can support
alternative character sets." at
http://www.w3.org/TR/REC-html32#textarea, but this does not say
anything about the value before submission.
For textarea, line endings are typically CrLf, as indicated in the above
excerpt. As others have indicated, attempts to catch the CrLf before
submission will fail if the client has scripting disabled.

Perhaps we can help out if you would explain why you need to detect line
endings prior to submission.
 
G

Gene Wirchenko

For textarea, line endings are typically CrLf, as indicated in the above
excerpt. As others have indicated, attempts to catch the CrLf before
submission will fail if the client has scripting disabled.

That is not an issue. Scripting will be enabled. This is for an
internal app.
Perhaps we can help out if you would explain why you need to detect line
endings prior to submission.

I covered that: "I need to be able to do some clean up on the
entered value on the frontend". I value a good UI. Shoving a mess to
the server is not the way to do it.

Sincerely,

Gene Wirchenko
 
N

Neil Gould

Hi Gene,

Gene said:
I covered that: "I need to be able to do some clean up on the
entered value on the frontend". I value a good UI. Shoving a mess to
the server is not the way to do it.
Trying to clean it up as it's being typed also uses the server (to run your
JavaScript), so it still has to deal with the same "mess", and since running
a parsing loop is a far less efficient process, I'd still recommend parsing
the form content on submission.
 
T

Tim Streater

Neil Gould said:
Hi Gene,


Trying to clean it up as it's being typed also uses the server (to run your
JavaScript),

This is incorrect. He can run JS client-side to do cleanup.
 
G

Gene Wirchenko

No, it uses the workstation.
This is incorrect. He can run JS client-side to do cleanup.

Quite, and this processing is comparatively cheap. The computer
is likely just sitting there running the page and not much else.

Oh, I plan to. I expect the validation at the server end will
generally go much faster since, usually, nothing will have to be done
to clean the data. I suppose that an untrusting sort might log bad
data getting to the server and consider checking whether it is a
front-end bug or a hacking attempt, but I will leave that game for
someone else to play.

Sincerely,

Gene Wirchenko
 
N

Neil Gould

Tim said:
This is incorrect. He can run JS client-side to do cleanup.
Yikes... what a brain fart I had! Sorry if anyone was confused by my
statement. =8-0
 

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
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top