>I have a textarea where a user can enter lines of text. My client wants the
>text limited to 500 characters.
>
This question seems to pop up every other month or so, which means the
archives is probably chalk full of solutions.
Here's a repeat of what I posted the last time this came up. If this
doesn't suit your needs then check the archives for other solutions.
function EnforceMaxChars(elObj, maxChars) {
// elObj is an object reference to the text area element
// maxChars is the maximum to allow
if (elObj.value.length > maxChars) {
// truncate the value and alert the user
var intLost = elObj.value.length - maxChars;
elObj.value = elObj.value.substring(0, maxChars);
alert(intLost + ' characters were truncated.');
}
}
Call this function when the onChange event for the text area (or even
text box) triggers:
<TEXTAREA NAME="blah" onChange="EnforceMaxChars(this, 500);">
If the user enters more than the maxChars number of characters when they
leave the textarea they will get an alert box telling them how many were
lost and then the value will be truncated. Of course, you may want to make
the message a little more user friendly <g>.
You could also use the onKeyDown or onKeyPress events to check the length as
each key is
pressed, but this limits you to the version 4 browsers. The script above
should work on just about any JS enabled browser.
Regards,
Seth Petry-Johnson
Argo Enterprise and Associates
------------------------------------------------------------------------------
Archives: http://www.eGroups.com/list/cf-talk
To Unsubscribe visit
http://www.houseoffusion.com/index.cfm?sidebar=lists&body=lists/cf_talk or send a
message to [EMAIL PROTECTED] with 'unsubscribe' in the body.