• Post Reply Bookmark Topic Watch Topic
  • New Topic

Writing length Validation for Text Area Fields in HTML  RSS feed

 
Singar Vel
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a text area field in which one should not be able to enter more than 70 characters.
Actually for Text Field using MAXLENGTH property I can control the maximum characters for a particular field. Similarly if I have a text area is there something like a Maxlength to decide the length or do I need to write some Java Script for this. If that is so what should I write.
Thanks and Regards,
singar
 
Srividya Shiv
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something like this should work.
<SCRIPT LANGUAGE="JavaScript">
<!--
function maxlength(max){
if (document.form.desc.value.length >= max) {
document.form.desc.value = document.form.desc.value.substring
(0,max);
alert (max+" MAX exceeded!");
return false;
}
}
-->
</script>
<form name="form">
<textarea name="desc" COLS="60" onkeypress="maxlength
(500)">some text....</textarea>
</form>
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just copied this from my site, this can keep it from being pasted into the text area,
Add this code between the head tags of your page:
<head>
<script>
function CharLimit(Element,MaxChars){
if(Element.value.length>=MaxChars){
Element.value=Element.value.slice(0,MaxChars);
return false;
}
}
</script>
</head>
Now this is what you need to add to your text element's tags
<input type="text" name="A1" size='20' onkeypress="return CharLimit(this,10)" onchange="CharLimit(this,10)">
Note: Change the number 10 in the onchange and onkeypress to your max number of characters you wish to have!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!