HTML form character counter

Discussion in 'JavaScript' started by Weirfire, Apr 25, 2006.

  1. #1
    I've got this Javascript function which stops you from typing any more than X characters but what I'd like to do is actually display the characters going up as the person types.

    The url to what works now is www.weirfire.co.uk/form.php

    and the code I've got so far is

    
    <script language="JavaScript">
    maxKeys = 100;
    keysSoFar = 0;
    alerted = false;
    
    function keyup(what) {
        keysSoFar++;
        if (keysSoFar > maxKeys) {
            if (!alerted) alert('That\'s enough!');
            what.value = what.value.substring(0,maxKeys-1); // chop the last typed char
            alerted = true;
        }
    }
    </script>
    
    <form>
    <textarea cols=100 rows=20 onChange="change(this)" onKeyUp="keyup(this)"></textarea>
    </form>
    
    Code (markup):
     
    Weirfire, Apr 25, 2006 IP
    obenix likes this.
  2. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #2
    okay I've found another solution

    
    <script type="text/javascript">
    function getObject(obj) {
      var theObj;
      if(document.all) {
        if(typeof obj=="string") {
          return document.all(obj);
        } else {
          return obj.style;
        }
      }
      if(document.getElementById) {
        if(typeof obj=="string") {
          return document.getElementById(obj);
        } else {
          return obj.style;
        }
      }
      return null;
    }
    
    function Counter(start,end,text,characters) {
      var startObj=getObject(start);
      var endObj=getObject(end);
      var longitude=characters - startObj.value.length;
      if(longitude <= 0) {
        longitude=0;
        text='<span class="disable"> '+text+' </span>';
        startObj.value=startObj.value.substr(0,characters);
      }
      endObj.innerHTML = text.replace("{CHAR}",longitude);
    }
    </script>
    
    
    <form action="#" method="post">
    <table align="center" class="0" border="0" cellspacing="1" cellpadding="5">
      <tr>
        <td class="2">
    <INPUT TYPE="TEXT" class="text" id="eBann" name="bannerURL" maxlength="100" size="60" 
    onKeyUp="Counter('eBann','sBann','{CHAR} characters left.',100);"><br>
    <span id="sBann" class="minitext">100 characters left.</span></td>
      </tr>
    </table>
    </form>
    
    Code (markup):

    That seems to do the trick :)
     
    Weirfire, Apr 25, 2006 IP
  3. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #3
    In the getObject function, if obj, is not a string, you should probably return obj and not obj.style
     
    exam, Apr 25, 2006 IP
  4. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #4
    Thanks for the tip D :)
     
    Weirfire, Apr 26, 2006 IP
  5. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #5
    exam, Apr 26, 2006 IP
  6. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #6
    Now you're just showing off! :p
     
    Weirfire, Apr 26, 2006 IP
  7. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #7
    exam, Apr 26, 2006 IP
  8. kashem

    kashem Banned

    Messages:
    1,250
    Likes Received:
    76
    Best Answers:
    0
    Trophy Points:
    0
    #8
    thanks guys I was in need of such code
     
    kashem, Apr 26, 2006 IP
  9. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #9
    You're welcome kashem. Donations are welcome! ;)
     
    Weirfire, Apr 26, 2006 IP