How can I limit the length, in characters, of a <textarea> field. I have tried the MAXLENGTH= property, but it seems to have no effect.
Thanks
Jez
Printable View
How can I limit the length, in characters, of a <textarea> field. I have tried the MAXLENGTH= property, but it seems to have no effect.
Thanks
Jez
In the onKeyDown event, call a function that checks the length of the value of the textarea.
HTHCode://let's suppose, maxlimit = 100
if (textareaname.value.length > maxlimit)
textareaname.value = textareaname.value.substring(0, maxlimit);
Yeah, I was trying this:
but your code is robably better.Code:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript">
function doIt(_obj) {
/*
Couldnd't get this working:
if (the_key_pressed_in_ascii == 8) //Backspace
{
return true
}
*/
if (_obj.value.length > 15)
{
return false
}
}
</script>
</head>
<body>
<textarea onKeyPress="return doIt(this)"></textarea>
</body>
</html>
I'm guessing that's Javascript.
Could you provide a simple explaination of how to put that code into a function and how to call it on the onKeyDown event please?
Javascript is confusing to me :ehh:
using menhaks code:
Code:
<script type="text/javascript">
function doIt(_obj) {
//let's suppose, maxlimit = 100
maxlimit = 100
if (_obj.value.length > maxlimit)
_obj.value = _obj.value.substring(0, maxlimit);
}
</script>
<textarea onKeyDown="return doIt(this)"></textarea>
Thanks, it works great.