RES: [Javascript] Maxlength on textarea in XHTML

Shawn Milo shawn.milo at gmail.com
Thu Apr 14 14:05:34 CDT 2005


I believe that this won't work, because it requires the maxLength to
be set in each textarea, which the OP, I believe, wanted to avoid.

Anyway, if you set the maxLength in each textarea, then why bother adding JS?
(objTA.value.length>=objTA.maxLength) can never be true, except for
the = part, which would make no difference.

Milo


On 4/14/05, Allard Schripsema <allard-schripsema at procergs.rs.gov.br> wrote:
> <html>
> <head>
> <title>Untitled</title>
> Is this what you need?
> </head>
> <script>
> function check(objTA){
>         if (objTA.value.length>=objTA.maxLength){
>                 objTA.value=objTA.value.substring(0,objTA.maxLength)
>                 alert("overflow")
>                 return false
>         }
> }
> </script>
> <body>
> <textarea id= test onkeypress="check(this)" onblur="check(this)"
> maxLength="10"></textarea>
> </body>
> </html>
> 
> allard
> -----Mensagem original-----
> De: javascript-bounces at LaTech.edu
> [mailto:javascript-bounces at LaTech.edu]Em nome de Charles Stuart
> Enviada em: quinta-feira, 14 de abril de 2005 13:15
> Para: [JavaScript List]
> Assunto: [Javascript] Maxlength on textarea in XHTML
> 
> Hello All,
> 
> I need to set a maxlength on all of the textarea's on a site. I am
> using XHTML 1.0 transitional. I've tried without success to implement
> Peter-Paul Koch's code located here:
> http://www.alistapart.com/authors/peterpaulkoch/
> 
> My needs are simpler than what is presented on A List Apart. The
> maxlength will always be the same value. And, don't mind having
>         onkeypress="checkLength()"
> in the XHTML.
> 
> So far I have this:
> 
> var x = document.getElementsByTagName('textarea');
> for (var i=0;i<x.length;i++)
> {
>         x[i].onkeypress = checkLength(x);
> }
> 
> function checkLength()
> {
>   var howMany = document.getElementsByTagName('textarea');
>   var max = 30;
> 
>   if (howMany.length > max)
>    {
>         window.alert("You can only type 30 characters");
>    }
> }
> 
> My second attempt was this:
> 
> function checkLength()
> {
>         event = window.event;
>         element = event.srcElement;
>         var howMany = document.forms[0].element.length;
> 
>         if (howMany.length > 30)
> {
>         window.alert("You can only type 300 characters");
> }
> }
> 
> Thank you for any help. Let me know if I should put an example up.
> 
> best,
> 
> Charles
> 
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
> 
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
> 


-- 
Voicemail any time at:
206-666-MILO



More information about the Javascript mailing list