dynamically count characters in a textarea as you type

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • beary
    New Member
    • Nov 2006
    • 170

    dynamically count characters in a textarea as you type

    Since php seems to be able to do pretty much anything, is it possible for it to calculate the number of characters or words in a textarea as the user types?

    Eg. there is a 300 word limit, and as the user types, a box changes the number of words remaining to the user after each word is typed.

    [eg. after typing the above sentence, the box would say 272.]

    Possible with php?
  • pritipshah
    New Member
    • Sep 2007
    • 11

    #2
    Hey beary

    You can put Javascript to call function below the text area.and pass NAME/ID of text area and max limit for the textarea in passing value of that funtion.

    Here is a function for count the limit for the text area.

    Code:
    function displaylimit(thename, theid, thelimit){
    var theform=theid!=""? document.getElementById(theid) : thename
    var limit_text='<b><span id="'+theform.toString()+'">'+thelimit+'</span></b> characters remaining on your input limit'
    if (document.all||ns6)
    document.write(limit_text)
    if (document.all){
    eval(theform).onkeypress=function(){ return restrictinput(thelimit,event,theform)}
    eval(theform).onkeyup=function(){ countlimit(thelimit,event,theform)}
    }
    else if (ns6){
    document.body.addEventListener('keypress', function(event) { restrictinput(thelimit,event,theform) }, true); 
    document.body.addEventListener('keyup', function(event) { countlimit(thelimit,event,theform) }, true); 
    }
    }

    Comment

    • beary
      New Member
      • Nov 2006
      • 170

      #3
      Originally posted by pritipshah
      Hey beary

      You can put Javascript to call function below the text area.and pass NAME/ID of text area and max limit for the textarea in passing value of that funtion.

      Here is a function for count the limit for the text area.
      Hey pritipshah, Thanks for replying! I must admit I'm much better at php than js. Here's what I have: in the head of the page

      <script type="text/javascript">
      ...your function as given...
      </script>

      Then I have <textarea name=comments id=counter etc>
      Then below that I have

      <SCRIPT LANGUAGE="JavaS cript" TYPE="TEXT/JAVASCRIPT">
      displaylimit(co mments, counter, 100);
      </SCRIPT>

      But nothing happens. In FF, there's just nothing. In IE6, there's a little yellow thing at the bottom left, which I think means something wrong. I'm sure it's something I've done wrong. Could you point me in the right direction please?

      Many thanks,

      Comment

      • RuthC
        New Member
        • Nov 2007
        • 33

        #4
        Hi beary,

        Try This ..

        Code:
        <script language="javascript">
        // to set lenth of text area 
        function imposeMaxLength(Object, MaxLen)
        {
          return (Object.value.length <= (MaxLen));
        }
        // to display the text areas length 
        function len_display(Object,MaxLen,element){
        	var len_remain = MaxLen-Object.value.length;
        	if(len_remain >=0){
        	document.getElementById(element).value=len_remain; }
        }
        </script>
        
        <textarea class="textarea1" cols="75" rows="10" name="long_desc" onkeypress="return imposeMaxLength(this, 1024);" onKeyUp="len_display(this,1024,'long_len')"></textarea>
        <input type="text" id="long_len" value="1024" class="len"> Charecters

        Regards
        Ruth

        Comment

        • beary
          New Member
          • Nov 2006
          • 170

          #5
          Originally posted by RuthC
          Hi beary,

          Try This ..

          Code:
          <script language="javascript">
          // to set lenth of text area 
          function imposeMaxLength(Object, MaxLen)
          {
            return (Object.value.length <= (MaxLen));
          }
          // to display the text areas length 
          function len_display(Object,MaxLen,element){
          	var len_remain = MaxLen-Object.value.length;
          	if(len_remain >=0){
          	document.getElementById(element).value=len_remain; }
          }
          </script>
          
          <textarea class="textarea1" cols="75" rows="10" name="long_desc" onkeypress="return imposeMaxLength(this, 1024);" onKeyUp="len_display(this,1024,'long_len')"></textarea>
          <input type="text" id="long_len" value="1024" class="len"> Charecters

          Regards
          Ruth
          My goodness that's nice! Thanks heaps Ruth. Exactly what I need. Thanks again.

          Comment

          Working...