General discussion

Locked

Limiting Text in a Textarea

By Maxwell Powers ·
I've been trying to limit the amount of text that can be actually and visibly written into a textarea text box. Much like the ones on Lycos free SMS and Vizzavi.co.uk free SMS etc (without the countdown box)... I've got as far as the following code but I get an alert box each time I type a character in the text box, which lets me know I have typed a character.

Can anyone point out where I have gone wrong, if it is possible to remove the number indicator alert, and how to do it.

Muchas Gracias

MP

This conversation is currently closed to new comments.

7 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

Limiting Text in a Textarea

by Maxwell Powers In reply to Limiting Text in a Textar ...

Ah, I forgot something:) here is what I am currrently using

<head>
<title>Text Area</title>
</head>

<body>
<script>
var IsMSIE = document.all;
function limit_textarea(ctl,iLimit) {
alert(ctl.value.length);
if (ctl.value.length >= iLimit) {
ctl.value = ctl.value.substr(0,iLimit);
alert("No more than "+iLimit+" characters are allowed.");
if (IsMSIE) {
} return false;
}
}
</script>


<p><textarea NAME="textarea1" ROWS="7" COLS="18" onkeypress="limit_textarea(this,10)"
WRAP="PHYSICAL" style="overflow:hidden"> </textarea></p>
</body>
</html>

Collapse -

Limiting Text in a Textarea

by CrshNBrn In reply to Limiting Text in a Textar ...

Just comment out line 3 in your code:

<script>
var IsMSIE = document.all;
function limit_textarea(ctl,iLimit)
{
//alert(ctl.value.length);
if (ctl.value.length >= iLimit)
{
ctl.value = ctl.value.substr(0,iLimit);
alert("No more than "+iLimit+" characters are allowed.");
if (IsMSIE)
{
}
return false;
}
}


Problem with your code: It allows 11 characters when I specify 10. Seems that it handles the keypress, fires the function limits the text to 10 and then allows he character to be entered anyhow.

I would try this:

<html>
<head>
<title>Text Area</title>
</head>

<body>
<script>
var IsMSIE = document.all;
function limit_textarea(ctl,iLimit)
{
//alert(ctl.value.length);
if (ctl.value.length > iLimit)
{
ctl.value = ctl.value.substr(0,iLimit);
alert("No more than "+iLimit+" characters are allowed.");
if (IsMSIE)
{
}
return false;
}
}
</script>


<p><textarea NAME="textarea1" ROWS="7" COLS="18" onkeyup="limit_textarea(this,10)"
WRAP="PHYSICAL" style="overflow:hidden"> </textarea></p>
</body>
</html>


Notice the onKeyUp event instead of the onkeypress. This ensures that the character is entered and handles the event appropriately. Also changed the >=iLimit to > iLimit to ensure that it only fires if the user has entered more than 10 characters.

Collapse -

Limiting Text in a Textarea

by Maxwell Powers In reply to Limiting Text in a Textar ...

The question was auto-closed by TechRepublic

Collapse -

Limiting Text in a Textarea

by Kanto Boy In reply to Limiting Text in a Textar ...

Hmm . . . ok form the looks of your code its compatible with MS IE only. So I guess this might help you. Set the maxlength attribute of textarea to the number of characters you want. This has the advantage of working even if javascript is turned off.

ex. <textarea maxlength="10000"></textarea>

Difference would be that the user wouldnt get the message box everytime he pressed a key.

Collapse -

Limiting Text in a Textarea

by Maxwell Powers In reply to Limiting Text in a Textar ...

The question was auto-closed by TechRepublic

Collapse -

Limiting Text in a Textarea

by Maxwell Powers In reply to Limiting Text in a Textar ...

This question was auto closed due to inactivity

Back to Desktop Forum
7 total posts (Page 1 of 1)  

Related Discussions

Hardware Forums