View Full Version : Limit a textarea to 1000 characters.
ventura
01-27-2004, 07:30 PM
I just need to do a simple validation of a <textarea> that it can only have a maximum of 1000 characters. Any character, space, etc. is okay.
squirellplaying
01-27-2004, 08:09 PM
<textarea maxlength="1000"> Will only allow users to enter in 1000 characters.
ventura
01-27-2004, 08:10 PM
textareas don't accept the maxlength.
squirellplaying
01-27-2004, 08:12 PM
Sorry, didn't think it was that easy.
SlySecretSpy
01-27-2004, 10:25 PM
function textareaupdate()
{
if (eval(document.getElementById('textareainformation').value.length+1) > 1001)
{
document.getElementById('textareainformation').value = document.getElementById('textareainformation').value.substring(0,1000);
}
}
<textarea cols="25" rows="2" onKeyPress="textareaupdate()" id="textareainformation"></textarea>
Philip M
01-28-2004, 07:34 AM
Here's what I use and it tells the user how many characters are left. Also cuts out nasty < and >! (i.e. no HTML can be entered).
function filterHTML(str) {
nohtml = /\<|>/g; // remove "<" and ">" from Description box
return str.replace(nohtml, "");
}
maxLen = 500; // max number of characters allowed in the textbox
function checkMaxInput(form) {
if (form.Description.value.length > maxLen) // if too long.... trim it!
form.Description.value = form.Description.value.substring(0, maxLen);
// otherwise, update 'characters left' counter
else form.remLen.value = maxLen - form.Description.value.length;
}
<textarea name=Description font face="ARIAL" cols="60" rows="4" wrap=virtual onKeyDown="checkMaxInput(this.form)" onKeyUp="checkMaxInput(this.form)" onBlur="this.form.Description.value=filterHTML(this.form.Description.value) "></textarea>
<br>
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.