View Full Version : Form: count # of words * $ amount = ?
mattboy_slim
12-19-2002, 03:42 PM
I have been searching high and low for a javascript that will take the number of words within a text box, and tell me how many words were typed in there, and I found at the great JavaScriptkit.com.
Exact location:
http://javascriptkit.com/script/script2/countwords.shtml
What I'm looking for exactly, is someone to help me take what is there, and expand it.
What I would like the script to do, is to calculate the number of words, and if the number of words is over 30, then take that number of words over 30, and multiply that number times $.0.25, and output a total dollar amount.
Example: I want a 35 word classified ad. The price is $6.00 for the first 30 words, and $0.25 after that. So the add would cost me $6.00 + $1.25 = $7.25. I would like the output to say the ad is going to cost them $7.25.
This is simply so the end-user knows what the ad is going to cost them before they submit the form.
Here is the form I need to incorporate this into:
http://www.lakesnewsshopper.com/submit.asp
Thanks in advance for the help. Everything you folks do is appreciated by us "coding-stupid" people out here.
Matt
Spookster
12-19-2002, 04:05 PM
Since you have access to ASP I would do it using that verses javascript since a person can either turn off javascript or may not have a javascript capable browser.
As far as the logic goes here is one I wrote in PHP:
http://codingforums.com/showthread.php?s=&threadid=11649
The same logic will apply to any language it is written in.
mattboy_slim
12-19-2002, 04:11 PM
Couple of problems. Not an option to host on a PHP server, since the newspaper will be hosting it themselves.
I've also never touched doing math in ASP. My ASP knowledge lies pretty much within database work only.
Skyzyx
12-19-2002, 04:29 PM
Something like this should work, although I haven't actually tested it yet.
Looking at the following code...
<form name="form1" method="post" action="thanks.asp">
...
<textarea name="textfield6" cols="40" rows="4">
...
// Add this...
<input type="text" name="amount" value="">
...
<input type="submit" name="Submit" value="Submit Your Ad">
You could do something like this:
function chargeWords()
{
// This will get a number of total words.
wordCount=document.form1.textfield6.value.split(" ");
// This will find out how many words are over 30.
chargeWordCount=wordCount-30;
// If there are no words over 30, there is no charge.
// Otherwise, charge 25 cents per page.
if (chargeWordCount < 1) chargeAmount=0;
else chargeAmount=chargeWordCount*0.25;
// This will make sure is is in monetary format without using a FIX command.
if (parseFloat(chargeAmount).toString().indexOf('.') != -1) document.form1.amount.value='$'+chargeAmount;
else document.form1.amount.value='$'+chargeAmount+'.00';
}
mattboy_slim
12-19-2002, 04:46 PM
Wow, thanks Skyzyx. you don't know how much I appreciate this. My uncle will be happy when I get this working (It's his web site).
I am having some trouble implementing your script though, which you can see here:
http://www.lakesnewsshopper.com/submit2.asp
Please let me know what I'm doing wrong.
EDIT:
The text that appears in the "Cost of ad" box is: $NaN.00
EDIT #2:
Would it be easier to have the actual form submission box open a popup window and show the price, or do you think how I have it is fine?
mattboy_slim
12-20-2002, 03:51 PM
Sorry, I need to bump this up so it doesn't get forgotten.
Tanker
12-20-2002, 06:36 PM
The reason you were getting nan is that he was trying to use wordCount as a number when it actually contained the text that it parsed from the textbox. I expanded on what he was doing, if your interested here is what I did....
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Word Counter</title>
<script language="JavaScript1.1">
<!--
function chargeWords()
{
frm = document.form1
chargeWordCount = 0 //0 out chargeWordCount before re-calculating them
wordLimit = 30 //Sets number of words before addt'l charges are added
wordCost = .25 //Sets cost of addt'l words over wordLimit
baseCost = 6 //Sets base cost of add
// This will get a number of total words.
wordCount=frm.textfield6.value.split(" ");
frm.words.value = wordCount.length
// This will find out how many words are over 30.
if(wordCount.length >= wordLimit){
chargeWordCount=wordCount.length-wordLimit;
} else {
chargeWordCount = 0
}
frm.wordsOver.value = chargeWordCount;
// If there are no words over 30, there is no charge.
// Otherwise, charge 25 cents per page.
if (chargeWordCount < 1) chargeAmount=0.00;
else chargeAmount=chargeWordCount*wordCost;
// This will make sure is is in monetary format without using a FIX command.
if (parseFloat(chargeAmount).toString().indexOf('.') != -1) frm.addOver.value='$'+chargeAmount;
else frm.addOver.value='$'+chargeAmount+'.00';
//Add the $6.00 base charge
totalCost = parseFloat(baseCost) + parseFloat(chargeAmount)
if (parseFloat(totalCost).toString().indexOf('.') != -1) frm.amount.value='$'+totalCost;
else frm.amount.value='$'+totalCost+'.00';
}
//-->
</script>
</head>
<body>
<table align="center" cellspacing="2" cellpadding="2" border="1">
<form name="form1" method="post">
<tr>
<td rowspan="4"><textarea name="textfield6" cols="40" rows="7">This is the default text.</textarea></td>
<td>Words: </td><td> <input type="text" name="words" size="4" onFocus="blur();"></td>
</tr>
<tr><td>Over 30: </td><td> <input type="text" name="wordsOver" value="" size="4" onFocus="blur();"></td></tr>
<tr><td>Add: </td><td> <input type="text" name="addOver" value="" size="4" onFocus="blur();"></td></tr>
<tr><td>Cost: </td><td> <input type="text" name="amount" value="" size="4" onFocus="blur();"></td></tr>
<tr><td colspan="3"><input type="Button" name="Submit" value="Submit Your Ad" onClick="chargeWords();"></td></tr>
</form>
</table>
</body>
</html>
mattboy_slim
12-23-2002, 10:43 PM
Hey Tanker, thanks so much for the help. I got that implemented into my current form and the calculations work perfectly.
I have a sort of unrelated question though. LINK:
http://www.lakesnewsshopper.com/SUBMIT2.ASP
How, when the value is, say $7.50, do I make it show the zero, rather than $7.5 ? Is that possible? Please let me know if you can help.
LINK AGAIN:
http://www.lakesnewsshopper.com/SUBMIT2.ASP
Thanks,
Matt
Skyzyx
12-24-2002, 03:01 AM
Try this...
if (parseFloat(chargeAmount).toString().indexOf('.') != -1)
{
temp=parseFloat(chargeAmount).toString();
temp2=temp.split('.');
if (temp2[1].length==2) frm.addOver.value='$'+chargeAmount;
else if (temp2[1].length==1) frm.addOver.value='$'+chargeAmount+'0';
}
else frm.addOver.value='$'+chargeAmount+'.00';
mattboy_slim
12-24-2002, 03:02 PM
Skyzyx, I'm not sure I implemented that right. I'm still not getting a zero after the .50 denominations.
See link:
http://www.lakesnewsshopper.com/SUBMIT2.ASP
Skyzyx
12-24-2002, 04:47 PM
Well, the scripts work now... I modified some of your code slightly, so it works now... mostly. There is something that is writing "undefined" to that input box, and I can't find it.
I've set it up as an alert instead. It works like that. There might be another script somewhere that is messing up that input box.
Also, you didn't need my last post with that formatCurrency() function you've got. I might have to steal that! ;)
Anyways, I've posted the modified code at http://skyzyx.freehomepage.com/lns/lakesnewsshopper.htm
You can view source and take the code.
mattboy_slim
12-27-2002, 02:30 PM
Thanks for all the help. Everything works great now. The help provided on this board has been invaluable, and I can assure you this will not be my only stop here.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.