• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Writing length Validation for Text Area Fields in HTML

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic