Limit textarea alert message

T

teser3

I have the below that limits the textarea input to 500 characters but
cant get the alert message to work. It doesnt show anything. Please
advise.
<script language="javascript" type="text/javascript">
function limitText(limitField, limitCount, limitNum) {
if (limitField.value.length > limitNum) {
limitField.value = limitField.value.substring(0, limitNum);
alert("You are trying to enter more
than 500 characters");
} else {
limitCount.value = limitNum - limitField.value.length;
}
}
</script>
--------------------------------------------------------------------------------

<form name="myform">
<textarea name="limitedtextarea"
onKeyDown="limitText(this.form.limitedtextarea,this.form.countdown,
500);"
onKeyUp="limitText(this.form.limitedtextarea,this.form.countdown,
500);">
</textarea>
 
L

Lasse Reichstein Nielsen

I have the below that limits the textarea input to 500 characters but
cant get the alert message to work. It doesnt show anything.

Do you get any error messages from the browser?

Does the line previous to the alert work? I.e., is the textarea
contents restricted to 500 chars?
alert("You are trying to enter more
than 500 characters");

The linebreak is not allowed in a string. If this is your actual code,
that could be the problem.

/L
 
E

Evertjan.

I have the below that limits the textarea input to 500 characters but
cant get the alert message to work. It doesnt show anything. Please
advise.
<script language="javascript" type="text/javascript">
function limitText(limitField, limitCount, limitNum) {
if (limitField.value.length > limitNum) {
limitField.value = limitField.value.substring(0, limitNum);
alert("You are trying to enter more
than 500 characters");

beware of linebraks!!!

the else is not needed.
limitCount.value = limitNum - limitField.value.length;
}
}
</script>
-----------------------------------------------------------------------
---------

<form name="myform">
<textarea name="limitedtextarea"
onKeyDown="limitText(this.form.limitedtextarea,this.form.countdown,
500);"

onKeyDown="limitText(this,this.form.countdown, 500);"

does the same.
[If you are John,
saying "the son of my father named John" is in effect
the same as saying "me".]

The onkeyup in fact is all that is needed.
onKeyUp="limitText(this.form.limitedtextarea,this.form.countdown,
500);">
</textarea>

This works fine:

==================
<script type='text/javascript'>
var max = 500; // or debug with 5
function limitText(limitField, limitCount, limitNum) {
if (limitField.value.length > limitNum) {
limitField.value = limitField.value.substring(0, limitNum);
alert('Do not enter more than '+ limitNum +' characters');
};
limitCount.value = limitNum - limitField.value.length;
};
</script>

<form name='myform'>
<textarea name='limitedtextarea'
onKeyUp='limitText(this,this.form.countdown,max);'>
</textarea>
<br><br><input name='countdown' readonly>
</form>
===================

Well, not perfect, if you add letters in the middle, another letter will
be deleted, the one at the end.

And not perfect, because you can add a long sentence by pasting [ctrl-V]

So additional tests have to be done, preferably onsubmit [and also
serverside testing, if Javascript is switched off, or if the clientside
code is manipulated.]

Depending on the OS, a <return> will count for two, or one character.
 
E

Evertjan.

Evertjan. wrote on 07 mei 2008 in comp.lang.javascript:
This works fine:

==================
<script type='text/javascript'>
var max = 500; // or debug with 5
function limitText(limitField, limitCount, limitNum) {
if (limitField.value.length > limitNum) {
limitField.value = limitField.value.substring(0, limitNum);
alert('Do not enter more than '+ limitNum +' characters');
};
limitCount.value = limitNum - limitField.value.length;
};
</script>

<form name='myform'>
<textarea name='limitedtextarea'
onKeyUp='limitText(this,this.form.countdown,max);'>
</textarea>
<br><br><input name='countdown' readonly>
</form>
===================

Well, not perfect, if you add letters in the middle, another letter
will be deleted, the one at the end.

And not perfect, because you can add a long sentence by pasting
[ctrl-V]

So additional tests have to be done, preferably onsubmit [and also
serverside testing, if Javascript is switched off, or if the
clientside code is manipulated.]

Try this, IE tested, the oncontextmenu seems IE specific:

================
<script type='text/javascript'>
var max =10;
function limitText(limitField, limitCount, limitNum) {
if (limitField.value.length > limitNum) {
limitField.value = limitField.old;
alert('Do not enter more than '+ limitNum +' characters');
};
limitCount.value = limitNum - limitField.value.length;
limitField.old = limitField.value;
};
</script>

<form name = 'myform'>
<textarea name = 'limitedtextarea'
onkeydown = 'this.old=this.value;'
onKeyUp = 'limitText(this,this.form.countdown,max);'
oncontextmenu = 'alert("No pasting please");return false;'>
</textarea>
<br><br><input name='countdown' readonly>
</form>
================

Ctrl-V is only allowed if the result is within limit.
 

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,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top