...

View Full Version : WordCount



Crash1hd
08-07-2002, 12:42 PM
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 :)

beetle
08-07-2002, 03:44 PM
The problem is with this line
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...
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.

mordred
08-07-2002, 04:46 PM
Not quite, you have to pass a Regular Expression object to the split function and you have to change the quantifier:



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.

beetle
08-07-2002, 04:49 PM
Thanks mordred...I knew I was a bit off....:D

. o 0 ( Need more coffee! )

Crash1hd
08-08-2002, 08:23 AM
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 :cool:

Did I say thankyou yet :thumbsup:

mordred
08-08-2002, 03:57 PM
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



<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?

whammy
08-08-2002, 05:32 PM
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:



<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>

Kylena
10-01-2003, 10:41 AM
Can the above be used to limit the number of words?


maxlength = 500 - x.value.length

And I can display the number of words left. Do I have to change anything else? :p



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum