VB icon

Count Words In a Texbox

Email
Submitted on: 1/5/2015 3:02:00 PM
By: Oliver French (from psc cd)  
Level: Beginner
User Rating: By 4 Users
Compatibility: ASP (Active Server Pages), HTML, VbScript (browser/client side)
Views: 1340
 
     This code will count the number of words in a textbox. A friend asked how to do this so I thought I'd post my reply.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Count Words In a Texbox
' Description:This code will count the number of words in a textbox. A friend asked how to do this so I thought I'd post my reply.
' By: Oliver French (from psc cd)
'
' Inputs:Any string.
'
' Returns:The number of words in the string.
'
' Assumes:Assumes a word is any set of characters separated by a space. The code can be easily modified to use any delineators.
'**************************************

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<SCRIPT LANGUAGE = VBSCRIPT>
<!--
sub AddWords()
	dim strWhole
	dim x
	dim i
	dim currentLetter
	dim prevLetter
	dim total
	
	strWhole = Trim(form1.txtString.value) ' Remove leading and trailing spaces of string if any.
	
	if strWhole = "" then		' The text box is empty.
		msgbox "There are no words in the text box!"
		exit sub
	end if
	
	x = Len(strWhole)		' Store the total number of letters in the string.
	
	total = 1		' Start the count at one.
	
	' Loop through the string two letters at a time. If the current letter is a space, and the previous
	' letter was not a space, then another word is counted. 
	For i = 2 to x	
		currentLetter = mid(strWhole, i, 1)
		prevLetter = mid(strWhole, i - 1, 1)
		if currentLetter = Chr(32) and prevLetter <> Chr(32) then
			total = total + 1
		end if
	next
	
	msgbox "There are " & total & " words in the text box."
end sub
' -->
</Script>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<p>Enter the string you wish counted:</p>
<form name="form1" method="post" action="">
 <p>
<textarea name=txtString rows=5 cols=51></textarea>
 </p>
 <p> </p>
 <p> 
<input type="button" name="cmdExecute" value="Add Words" onclick=AddWords()>
 </p>
 <p> </p>
</form>
</body>
</html>


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.