Advertisement
ASP_Volume3 Debugging and Error Handling #51682

Count Words In a Texbox

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.

AI

AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Source Code
original-source
<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>&nbsp;</p>
 <p> 
  <input type="button" name="cmdExecute" value="Add Words" onclick=AddWords()>
 </p>
 <p>&nbsp;</p>
</form>
</body>
</html>
Original Comments (3)
Recovered from Wayback Machine