Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word Count 1

Status
Not open for further replies.

gms27

Technical User
Aug 19, 2005
27
PT
I there!
I need to make a text area in my web site that the visitors will use to make a word count of a text inserted by them.
Can someone please help me with this?
 
<HEAD>

<SCRIPT LANGUAGE="JavaScript">

function CountWords (this_field, show_word_count, show_char_count) {
if (show_word_count == null) {
show_word_count = true;
}
if (show_char_count == null) {
show_char_count = false;
}
var char_count = this_field.value.length;
var fullStr = this_field.value + " ";
var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
var splitString = cleanedStr.split(" ");
var word_count = splitString.length -1;
if (fullStr.length <2) {
word_count = 0;
}
if (word_count == 1) {
wordOrWords = " word";
}
else {
wordOrWords = " words";
}
if (char_count == 1) {
charOrChars = " character";
} else {
charOrChars = " characters";
}
if (show_word_count & show_char_count) {
alert ("Word Count:\n" + " " + word_count + wordOrWords + "\n" + " " + char_count + charOrChars);
}
else {
if (show_word_count) {
alert ("Word Count: " + word_count + wordOrWords);
}
else {
if (show_char_count) {
alert ("Character Count: " + char_count + charOrChars);
}
}
}
return word_count;
}
// End -->
</script>

</HEAD>

<!-- STEP TWO: Copy this code into the BODY of your HTML document -->

<BODY>

<form>
<textarea cols=40 rows=5 name=x>
</textarea>
<br>
<input type=button value="Count Words" OnClick ="CountWords(this.form.x, true, true);">
</form>

<p><center>
<font face="arial, helvetica" size"-2">Free JavaScripts provided<br>
by <a href=" JavaScript Source</a></font>
</center><p>

i have found this code and put it to work. the only problem is that its counting numbers.
Can someone fix it please?
 
this?

:--------------------------------------:
fugitive.gif

[URL unfurl="true"]http://mostarnet.com[/url]

All around in my home town,
They tryin' to track me down...
 
oh and if you can make a code that multiplies the number of words by 0.08 and presents the result in a separate field please let me know!
Thanks in advance
 
No lebisol, what i'm searching for is a code that counts only words in a text area, like the code above, but the problem is that it also counts numbers...
 
ah my bad ...
not a JS guy but my inital guess would be to look into:

1. validate field so it may NOT contain a number=force users not to enter numerics (u can use DW form validation for this)

then

2. count all words

All the best!

:--------------------------------------:
fugitive.gif

[URL unfurl="true"]http://mostarnet.com[/url]

All around in my home town,
They tryin' to track me down...
 
thanks again lebisol ;)
its still counting numbers but i'll try somehow to keep them from counting.
thanks again, and if u can tell me some way to multiply the result of the wordcount by 0.08 please let me know.
cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top