Maximum characters in TextArea

M

Mr. x

Hello,
How can I limit the maximum characters that can be entered in the <textArea>
(using html attributes) ?

Thanks :)
 
N

Nico Schuyt

Mr. x said:
How can I limit the maximum characters that can be entered in the
<textArea> (using html attributes) ?

By reading a small booklet ($5) "HTML for starters"
You should have a least a basic knowledge before asking *everything* in a
newsgroup.
Nico
 
C

Chris Morris

Mr. x said:
How can I limit the maximum characters that can be entered in the <textArea>
(using html attributes) ?

Why do you need to? (as always, an example URL would be great, if
possible) If it's so a server-side process doesn't get given input too
long, you still need to validate server-side that it works if they go
over the limit, to defend yourself against other people writing
similar looking forms without that limitation and/or using browsers
that don't support that attribute.

You could politely ask them to keep their content concise, I suppose.
 
S

Steve Pugh

Mr. x said:
How can I limit the maximum characters that can be entered in the <textArea>
(using html attributes) ?

You can't using HTML attributes.

You have to check the length of the submission in the server side
script that accepts you form submission, and optionally in a client
side script (e.g. JavaScript) as well.

Steve
 
M

Mr. x

You can't using HTML attributes.
You have to check the length of the submission in the server side
script that accepts you form submission, and optionally in a client
side script (e.g. JavaScript) as well.
Is this the only way to do so (what if user enters milion of character - I
still need to check milion of characters.

Thanks :)
 
J

Jukka K. Korpela

Mr. x said:
I mean attributes of <textarea> element, that can limit the maximum
size of characters.

They are HTML attributes. The phrase "HTML attribute" stands for the
attribute of any element in HTML markup. The answer stands; there is no
attribute said:
Is this the only way to do so (what if user enters milion of
character - I still need to check milion of characters.

It is the only way. How you do that is a different thing. Your form
handler could first just look at the Content-Length header in HTTP
headers and discard the submission (politely, perhaps) if there is no
such header or its value is larger than some limit. You would still
need the code for actually processing the data when its size is
acceptable, and check the amount of data - since a cracker could have
faked Content-Length: 42 and still send you megabytes of junk.
This shouldn't be a problem, since the handler needs to process every
character anyway, and counting them (one way or another) is just a
minor side job to be done.

More info: http://www.cs.tut.fi/~jkorpela/forms/textarea.html
 
S

Steve Pugh

Mr. x said:
I mean attributes of <textarea> element, that can limit the maximum size of
characters.

I know what you meant and my answer applies.

You can not set a maximum length for the content of the textarea
element with any attribute in HTML.
Is this the only way to do so (what if user enters milion of character - I
still need to check milion of characters.

Yes.

Steve
 
C

Chris Morris

Mr. x said:
I mean attributes of <textarea> element, that can limit the maximum size of
characters.

There aren't any.
Is this the only way to do so (what if user enters milion of character - I
still need to check milion of characters.

In PHP it's trivial to do:

Say your maximum length is 300 chars and your textarea is called
'input', and the form is POSTed.

<?php
if (strlen($_POST['input']) > 300) {
// appropriate rejecting mechanism
} else {
// do what you want with it.
}
?>

Similar is possible (probably as easily) in other server-side languages.

And yes, it's the only safe way to do it. Otherwise there's nothing
to stop someone writing their own form and using that instead.
 
R

Richard

Why do you need to? (as always, an example URL would be great, if
possible) If it's so a server-side process doesn't get given input too
long, you still need to validate server-side that it works if they go
over the limit, to defend yourself against other people writing
similar looking forms without that limitation and/or using browsers
that don't support that attribute.
You could politely ask them to keep their content concise, I suppose.


www.abuse.charter.net
Limits text to 2,000 characters.
 
L

Louis Somers

Mr. x said:
Hello,
How can I limit the maximum characters that can be entered in the
<textArea> (using html attributes) ?

Thanks :)

With a single line ( input type="text" ) you can use maxlength="10" as
attribute. Not with a textarea.

With a textarea you will need some javascript. Here is an example:

---- javascript example ----

<html><head>

<script type="text/javascript" language="JavaScript">
function calcCharLeft(theForm)
{
var mytext = theForm.thetext.value;
var availChars = 140;
if (mytext.length > availChars) {
theForm.thetext.value = mytext.substring(0,availChars);
theForm.fChars.value = 0;
alert('Your text is too long');
theForm.thetext.focus();
}
else {
theForm.fChars.value = availChars - mytext.length;
}
}

</script>
<title>limit chars</title>
</head>
<body>
<form method="post" action="yoururl" name="myForm">
<textarea name="thetext" rows="7" cols="26" onkeypress="calcCharLeft
(this.form)" onkeyup="calcCharLeft(this.form)" onkeydown="calcCharLeft
(this.form)" onchange="calcCharLeft(this.form)"></textarea>
<input class="disabled" align="right" type="text" name="fChars"
size="4" value="140" disabled onfocus="this.form.msg.focus()">
</form>
</body></html>

---- end javascript example ----

Cheers,
Louis
 
S

Steve Pugh

Richard said:

404

Presumably you meant http://abuse.charter.net/
Limits text to 2,000 characters.

No it doesn't. I've just posted over 15,000 characters. I had
JavaScript enabled. No error message came back from the server.

They use:
<textarea [snip] maxlength=2000></textarea> which is not valid and is
not supported by any browser as far as I'm aware. It may be supported
by some browsers but relying on it would be highly foolish.

Steve
 
R

Richard

Steve! said:
No it doesn't. I've just posted over 15,000 characters. I had
JavaScript enabled. No error message came back from the server.
They use:
<textarea [snip] maxlength=2000></textarea> which is not valid and is
not supported by any browser as far as I'm aware. It may be supported
by some browsers but relying on it would be highly foolish.

Well you know those highly trained tech types right?
But I did post an overlength message one time and did get the error message.
So perhaps it only works in IE.
 
S

Steve Pugh

Richard said:
Steve! said:
No it doesn't. I've just posted over 15,000 characters. I had
JavaScript enabled. No error message came back from the server.
They use:
<textarea [snip] maxlength=2000></textarea> which is not valid and is
not supported by any browser as far as I'm aware. It may be supported
by some browsers but relying on it would be highly foolish.

Well you know those highly trained tech types right?

I know some of them. Not the ones responsible for the page in
question.
But I did post an overlength message one time and did get the error message.

An error message would result from client or server side scripting.
This HTML attribute, if it did anything at all, would presumably work
in the same way as the same attribute on input and simply not allow
any extra characters to be typed into the box, i.e. no error message.
So perhaps it only works in IE.

Not IE6, Win XP. Just posted 3000 characters, no error message.

Steve
 
S

Steve Pugh

Richard said:
I am God, you will do it my way or I'll zap you with a lightning bolt.
Capiche?

I don't know, what is your way? So far all you've posted to this
thread is the incorrect URL of a page that uses a non-existent
attribute that doesn't do what the OP wants. So what is your way,
after all I wouldn't want to get hit by a lightning bolt for not doing
it.

Steve
 
S

Sam Hughes

By reading a small booklet ($5) "HTML for starters"
You should have a least a basic knowledge before asking *everything* in a
newsgroup.
Nico

When brucie's Lack-of-Life Quotient first came about, nobody expected a
person to get a high LOLQ by asking questions and not knowing anything.
But I think we might have found a counterexample.
 
Joined
Oct 2, 2009
Messages
1
Reaction score
0
Javascript makes it easy

<html>
<head>
<script language="javascript" type="text/javascript">
function limitText(element,limit) {
if (element.value.length > limit) {
element.value = element.value.substring(0, limit);
}
}
</script>

</head>

<body>

<form>
<textarea onKeyUp="limitText(this,20);" onKeyDown="limitText(this,20);" name="question" cols="50" rows="5" ></textarea>
</form>
</body>
</html>
 

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
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top