TextArea Validation

S

Sunil Kulkarni

I need to validate my TextArea as follows:

1) IT SHOULD ALLOW FOR ONLY 5 COLUMNS
2) IT SHOULD ALLOW ONLY 65 CHARACTERS PER ROW

I need some kind of a JS validation for the same. I have tried using hard wrap
but that doesnt help completely.

Any help in this matter would be very much appreciated!
Thanks!
S Kulkarni
 
L

Lasse Reichstein Nielsen

I need to validate my TextArea as follows:

1) IT SHOULD ALLOW FOR ONLY 5 COLUMNS

I assume you mean rows :)
2) IT SHOULD ALLOW ONLY 65 CHARACTERS PER ROW

I need some kind of a JS validation for the same. I have tried using
hard wrap but that doesnt help completely.

A regular expression should be able to match zero to five lines of
zero to 65 characters, if you define the lines to be separated by
newlines ("\n").

If you want to look at automatic line wrapping, and count lines as
they are displayed, it gets ugly fast, but with some assumptions,
it should be manageable.

So, let's define a line to be up to 65 characters followed by a
newline characters, or just 65 characters followed by a newline, and
we want to check that a string contains at most 5 lines.

The final line can be up to 65 characters without being followed by a
newline.

A suitable regular expression is:
/^(.{0,65}\n|.{65}){0,4}.{0,65}\n?$/

However, the input of a textarea might not use a single character for
newlines. Both Opera and IE ends their lines with "\x0d\x0a"
(Carrige Return + newline, DOS EOL), where Mozilla uses "\x0a" (Just
newline, Typical Unix EOL). Maybe Macintosh browsers use "\x0d\x0a",
as that is the typical Apple (and Amiga) EOL sequence.

Anyway, the above RegExp won't work in Opera and IE, since "\x0d\x0a"
is two newline characters. The solution I can see, is to replace those
with a single newline first:

string.replace(/(\x0a\x0d|\x0d\x0a)/g,"\n");

Then test with the above RegExp.
Any help in this matter would be very much appreciated!

Hope this helps.
/L
 
D

DU

Sunil said:
I need to validate my TextArea as follows:

1) IT SHOULD ALLOW FOR ONLY 5 COLUMNS
2) IT SHOULD ALLOW ONLY 65 CHARACTERS PER ROW

I need some kind of a JS validation for the same. I have tried using hard wrap
but that doesnt help completely.

Any help in this matter would be very much appreciated!
Thanks!
S Kulkarni



What's wrong with:

<form action="">
<p><textarea name="TextareaName" rows="5" cols="65"
wrap="soft"></textarea></p>
</form>

The string submitted can later be rendered within a textarea with the
same (or other) cols and rows attribute values.

On the wrap attribute, MSDN
http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/wrap.asp
says:
soft: Default. Text is displayed with wordwrapping and submitted without
carriage returns and line feeds.
hard: Text is displayed with wordwrapping and submitted with soft
returns and line feeds.
off: Wordwrapping is disabled. The lines appear exactly as the user
types them.

DU
--
Javascript and Browser bugs:
http://www10.brinkster.com/doctorunclear/
- Resources, help and tips for Netscape 7.x users and Composer
- Interactive demos on Popup windows, music (audio/midi) in Netscape 7.x
http://www10.brinkster.com/doctorunclear/Netscape7/Netscape7Section.html
 
S

Sunil Kulkarni

Thankyou very much for the suggestions!

Lasse Reichstein Nielsen said:
I assume you mean rows :)


A regular expression should be able to match zero to five lines of
zero to 65 characters, if you define the lines to be separated by
newlines ("\n").

If you want to look at automatic line wrapping, and count lines as
they are displayed, it gets ugly fast, but with some assumptions,
it should be manageable.

So, let's define a line to be up to 65 characters followed by a
newline characters, or just 65 characters followed by a newline, and
we want to check that a string contains at most 5 lines.

The final line can be up to 65 characters without being followed by a
newline.

A suitable regular expression is:
/^(.{0,65}\n|.{65}){0,4}.{0,65}\n?$/

However, the input of a textarea might not use a single character for
newlines. Both Opera and IE ends their lines with "\x0d\x0a"
(Carrige Return + newline, DOS EOL), where Mozilla uses "\x0a" (Just
newline, Typical Unix EOL). Maybe Macintosh browsers use "\x0d\x0a",
as that is the typical Apple (and Amiga) EOL sequence.

Anyway, the above RegExp won't work in Opera and IE, since "\x0d\x0a"
is two newline characters. The solution I can see, is to replace those
with a single newline first:

string.replace(/(\x0a\x0d|\x0d\x0a)/g,"\n");

Then test with the above RegExp.


Hope this helps.
/L
 

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

Forum statistics

Threads
474,093
Messages
2,570,607
Members
47,227
Latest member
bluerose1

Latest Threads

Top