word counter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • reneecccwest

    word counter

    Hello, can anybody share the code for a word counter for the textarea
    user input? what if max characters are 256?

    thanks
  • Grant Wagner

    #2
    Re: word counter

    reneecccwest wrote:
    [color=blue]
    > Hello, can anybody share the code for a word counter for the textarea
    > user input? what if max characters are 256?
    >
    > thanks[/color]

    Assuming a "word" is any sequence of non-whitespace characters surrounded
    by whitespace, the following will count the words in a text area:

    <form>
    <textarea name="myTextare a"></textarea>
    <input type="button" name="myButton" value="Count words"
    onclick="countW ords(this.form. myTextarea.valu e);">
    </form>
    <script type="text/javascript">
    function countWords(s) {
    var words = s.split(/\s+/);
    alert(words.len gth);
    }
    </script>

    For preventing entry of more then 256 characters, you can use the
    following. Be aware you should provide server-side validation as well,
    since the client-side prevention shown below can easily be circumvented:

    <textarea name="myTextare a" onkeydown="retu rn maxChars(this,
    256);"></textarea>
    <script type="text/javascript">
    function maxChars(ta, count) {
    return (ta.value.lengt h < count);
    }
    </script>

    --
    | Grant Wagner <gwagner@agrico reunited.com>

    * Client-side Javascript and Netscape 4 DOM Reference available at:
    *


    * Internet Explorer DOM Reference available at:
    *
    Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.


    * Netscape 6/7 DOM Reference available at:
    * http://www.mozilla.org/docs/dom/domref/
    * Tips for upgrading JavaScript for Netscape 7 / Mozilla
    * http://www.mozilla.org/docs/web-deve...upgrade_2.html


    Comment

    Working...