How to create a multiline textbox with limited chars in HTML

Web programming topics
Post Reply
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

How to create a multiline textbox with limited chars in HTML

Post by Neo » Wed Feb 17, 2010 2:39 am

Code: Select all

<html>
   <head>
      <script>
         function LimitText(AId,BId)
         {
          var e1 = document.getElementById(AId);
          var e2 = document.getElementById(BId);
          var l1 = e1.value.length;
          (l1 > 32)?e1.value = e1.value.substring(0,32):e2.innerHTML = 32 - l1 
         }
      </script>
   </head>
   <body>
      <form>
        <label>Type text below</label>
        <textarea id="mtext" onkeyup="LimitText('mtext','cleft')" onblur="LimitText('mtext','cleft')"></textarea><br>
        <label id="cleft" style="color:red;font-weight:bold">32</label>
        <label style="color:red;font-weight:bold"> Characters Left</label>
      </form>
   </body>
<html>
Post Reply

Return to “Web programming”