423,688 Members | 2,486 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,688 IT Pros & Developers. It's quick & easy.

TextArea Validation

P: n/a
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
Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
ku********@hotmail.com (Sunil Kulkarni) writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

P: n/a
DU
Sunil Kulkarni wrote:
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/a...rties/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/doctorunc...e7Section.html

Jul 20 '05 #3

P: n/a
Thankyou very much for the suggestions!

Lasse Reichstein Nielsen <lr*@hotpop.com> wrote in message news:<ll**********@hotpop.com>...
ku********@hotmail.com (Sunil Kulkarni) writes:
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

Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.