Textarea validation,
How to limit the characters in a textarea
for not exceeding more than 50 characters.
<textarea rows="5" cols="15"></textarea>
Thanks.....
Textarea validation,
How to limit the characters in a textarea
for not exceeding more than 50 characters.
<textarea rows="5" cols="15"></textarea>
Thanks.....
One Google away.
<script language="javascript" type="text/javascript">
<!--
function imposeMaxLength(Object, MaxLen)
{
return (Object.value.length <= MaxLen);
}
-->
</script>
Implementation:
<textarea name="myName" onkeypress="return imposeMaxLength(this, 50);" ></textarea>
EDIT:
Code that doesn't freeze text:
<script type="text/javascript">
/***********************************************
* Textarea Maxlength script- © Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for legal use.
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/
function imposeMaxLength(obj){
var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
if (obj.getAttribute && obj.value.length>mlength)
obj.value=obj.value.substring(0,mlength)
}
</script>
<textarea maxlength="40" onkeyup="return ismaxlength(this)"></textarea>
Using:
<textarea rows="5" cols="15" maxlength="50"></textarea>
From http://sigswitch.com/2008/07/textarea-maxlength-with-jquery/:
$(document).ready(function(){
$('textarea[maxlength]').keyup(function(){
var max = parseInt($(this).attr(’maxlength’));
if($(this).val().length > max){
$(this).val($(this).val().substr(0, $(this).attr('maxlength')));
}
$(this).parent().find('.charsRemaining').html('You have ' + (max - $(this).val().length) + ' characters remaining');
});
});
I don't know of a built in HTML way but you can use this:
<textarea rows="5" cols="15" onkeydown="return validateCharLimit(this);"></textarea>
function validateCharLimit(area)
{
var limit = 50;
var iChars = area.value.length;
if (iChars > limit) {
return false;
}
return true;
}
<script>
function chkLen()
{
var tlen=document.getElementById('myTA').value.length;
if(tlen>50)
{
document.getElementById('myTA').value.substr(0,49)
}
}
</script>
<body>
<textarea id="myTA" onkeyup='chkLen()'></textarea>
</body>