Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: WordCount

  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Location
    51 03' -78" N -114 05' 72" W
    Posts
    617
    Thanks
    0
    Thanked 0 Times in 0 Posts

    WordCount

    Ok Somone on here gave me this Javascript (Thankyou)

    and it works great except!

    if for example you where to use this inside the box

    one two three

    and then moved outside the box it pops up 3 in a window which is great if you use

    one two three

    it still pops up six as there is more then 1 space in between two and three also if you use

    one two
    three

    it only sees two even though there is a third word in there

    now if I use

    one two
    three

    with the space it sees three and with 2 spaces it sees four!

    What I am asking for is really if someone can break down this javascript for me in plain english exactly what everything is doing and how it does it so that I can recreate it to make it work with multiple spaces and enters and or someone too re write it the same way it is now just too incorperate the spaces and enters yet still give an accurate reading

    Adam

    Script Attached below

    <script language="JavaScript" type="text/javascript">
    <!--
    /* Trim leading and trailing spaces from text entries */
    function trimtext(txt){
    var temptext=txt.replace(/\s*/,'');
    ets = temptext.substring(temptext.length-1, temptext.length);
    while(ets==" "){
    temptext = temptext.substring(0, temptext.length-1);
    ets = temptext.substring(temptext.length-1, temptext.length);
    }
    return temptext;
    }
    /* Count the number of words in the textarea */
    function count_words(x){
    var tempstr = trimtext(x.value);
    var temparray = tempstr.split(" ");
    var arylen = temparray.length;
    alert(arylen);
    }

    // -->

    </script>


    <textarea rows="5" cols="40" name="textarea" onblur=count_words(this) wrap="VIRTUAL"></textarea>


    Again I would Like to thank who ever gave me this script It has been awhile and I am unable to remember so I opoligize

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The problem is with this line
    Code:
    var temparray = tempstr.split(" ");
    The .split() method takes the string and creates and array, splitting it at every space. I *think* this change will work for you...
    Code:
    var temparray = tempstr.split("\s*");
    This, instead of looking for spaces, checks for any whitespace characters (spaces, tabs, carriage returns, newlines, etc) in any quantity.

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Not quite, you have to pass a Regular Expression object to the split function and you have to change the quantifier:

    Code:
    var temparray = tempstr.split(/\s+/);
    BTW, I think that's a very strange mixture of RegExp and ordinary string manipulation in that trimtext function... seems awkward to me... if it works, that's ok, I'd prefer doing it completely with RegExp.

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks mordred...I knew I was a bit off....

    . o 0 ( Need more coffee! )

  • #5
    Regular Coder
    Join Date
    Jul 2002
    Location
    51 03' -78" N -114 05' 72" W
    Posts
    617
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh My God Thankyou That did it lol

    Again Thankyou

    But Can someone still break the whole script down into lamens terms for me please as I want to use this with onClick or onSubmit but if I dont know how to use the script fully I cant change it

    Did I say thankyou yet
    Last edited by Crash1hd; 08-08-2002 at 07:26 AM.

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Can you be a little bit more specific where the actual problem lies? String manipulation? The Regular Expression syntax? Eventhandlers?

    If you want to call this script onclick or onsubmit, you call it similarly to what you have already in the onblur eventhandler. What you have to adjust is the parameter that contains the reference to the textarea, like

    Code:
    <a href="#" onclick="count_words(document.myFormName.myTextareaName.value); return false;">click</a>
    Where myFormName stands for the name attribute of your form, and myTextareaName... you get the picture.

    Does that help?

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    41 8' 52" N -95 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey, I like the regExp even better. I think I gave you that script, but of course, I got most of it from somewhere else, so it's all good - and you're right, mordred, you don't need all that stuff, this works great:

    Code:
    <script language="JavaScript" type="text/javascript"> 
    <!-- 
    /* Count the number of words in the textarea */ 
    function count_words(x){
    var temparray = x.value.split(/\s+/); 
    return(temparray.length); 
    }
    // -->
    </script>
    
    <textarea rows="5" cols="40" name="textarea" onblur="alert(count_words(this))" wrap="virtual"></textarea>
    Last edited by whammy; 08-08-2002 at 04:52 PM.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #8
    New Coder
    Join Date
    Aug 2003
    Location
    Singapore
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Can the above be used to limit the number of words?

    Code:
    maxlength = 500 - x.value.length
    And I can display the number of words left. Do I have to change anything else?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •