Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
23
rated 0 times [  25] [ 2]  / answers: 1 / hits: 34287  / 14 Years ago, thu, april 8, 2010, 12:00:00

Textarea validation,



How to limit the characters in a textarea for not exceeding more than 50 characters.



<textarea rows=5 cols=15></textarea>


Thanks.....


More From » jquery

 Answers
48

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) {
const mlength = obj.getAttribute ? parseInt(obj.getAttribute("maxlength")) : ""
if (obj.getAttribute && obj.value.length > mlength) {
obj.value = obj.value.substring(0, mlength)
}
obj.value = obj.value.trim()
}

</script>

<textarea maxlength="40" onkeyup="return imposeMaxLength(this)"></textarea>

[#97136] Sunday, April 4, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
haleeg

Total Points: 703
Total Questions: 100
Total Answers: 98

Location: Dominica
Member since Sat, Nov 5, 2022
2 Years ago
;