Counting Words (Full Version)

All Forums >> [Web Development] >> General Web Development



Message


FrogMan -> Counting Words (4/3/2001 20:15:00)

Okay, I am getting a little frustrated now.
I have been messing around with a site form inwhich I need to count the number of words that a visitor enters into a scolling text box (I have wrap=virtual ON). Thus far I have not found or been able to figure out how to count the "words". Once I know how many words there are in the text box I will set a range to calculate on the number of words a response; ie, if 10 words multlipy by 1, if 15 words multiply by 1 and add 3, if more than 15 words, multiply by .5.
Any Ideas?

------------------
Everyday is a brighter day, just a few are dimm.





Rian -> RE: Counting Words (4/3/2001 20:10:00)

How about counting characters?
***************************************

<body onLoad="document.myForm.smsInput.focus()">

<form NAME="myForm">

<input type="text" name="count" value="160" size="3" onFocus="this.blur" readonly>

<br>

<textarea rows="5" cols="50" name="smsInput" wrap
onKeyUp="
val = this.value;
if (val.length > 160) {
alert('Sorry, you are over the limit of 160 characters');
this.value = val.substring(0,160);
smsInput.focus()
}
this.form.count.value=160-parseInt(this.value.length);
"></textarea>

</form>

</body>


************************************


Rian

------------------
Webmaster
SR Web Creators
http://www.srwebcreators.com
_ _________________ _
"Press ENTER once to quit or twice to save changes..."
_ ________________ _





FrogMan -> RE: Counting Words (4/3/2001 20:28:00)

Thanks Rian.
This may just work and it gives me another thought. Counting the "Spaces" and adding "1" would work. In this manner it ends up with the same results. The problem I see with "just" counting characters, is the formula after the count is based on words. Think of a News paper "obituary". They charge based on the number of words, not characters.
But let me try your formula out and see what jells.
Thanks again.




Mojo -> RE: Counting Words (4/3/2001 23:43:00)

FrogMan,

I just wrote this. It is an ASP file, so you will need to be on an NT server...

It gives you a count of words seperated by spaces. If there are extra spaces on either end it removes those. But, if there are say two spaces between a word, it will count that as two words, so make sure your users understand to type correctly

You need to change "your_field_name" to what ever is the name of the field the user submits. "WordCount" is the variable that holds the final count. Here goes:

<%
Dim wordCount, arrWords, strOneWord
wordCount = 0
strSentence = Request("your_field_name")
strSentence = Trim(strSentence)
arrWords = Split(strSentence, " ", -1, 1)
For Each strOneWord in arrWords
wordCount = wordCount +1
Next
Response.Write(wordCount) & "<BR>"
%>

Joe

[This message has been edited by jbennett (edited 04-03-2001).]





FrogMan -> RE: Counting Words (4/3/2001 15:51:00)

Joe,

Thank you very much. This looks like what I originally had in mind. I'll let you know how it works out. It will be later this evening before I get too it, but I'll be checking back.





Mojo -> RE: Counting Words (4/4/2001 13:25:00)

FrogMan,

I saw a tip today that reminded me of your post. The below code will still give you a count of words seperated by spaces and will remove spaces at the beginning and end.

It will also remove any instance of double spaces. So if someone puts 2 spaces between a word like <--this it will remove one of the spaces. Also works for those people who put two spaces before a new sentence. Like me

code:

<%
Dim wordCount, arrWords, strOneWord
wordCount = 0
strSentence = Request("SearchText")
strSentence = Trim(strSentence)
Do While InStr(1, strSentence, " ")
strSentence = Replace(strSentence, " ", " ")
Loop
arrWords = Split(strSentence, " ", -1, 1)
For Each strOneWord in arrWords
wordCount = wordCount +1
Next
Response.Write(wordCount)
%>

[This message has been edited by jbennett (edited 04-04-2001).]

[This message has been edited by jbennett (edited 04-04-2001).]





FrogMan -> RE: Counting Words (4/4/2001 13:57:00)

Joe,

Thanks for the update. Actually have not had an opportunity to try the first (death in family), but I will be getting back on it before this weekend. The enhancement for not "counting" double spaces is excellent.

Thanks again





Page: [1]

Valid CSS!




Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI
0.0625