473,388 Members | 1,215 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Calculate Price Based On Quantity Range

I am trying to set up a page that calculates a total ad price based on a word count value. I have the word count displaying properly in op3, but just can't get the price even close to working right. If anybody has a clue, I'd appreciate the help. Here's what I have:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <script language="JavaScript" type="text/JavaScript"><!--
  3. // Word Count Function 
  4. function cnt(w,x){
  5. var y=w.value;
  6. var r = 0;
  7. a=y.replace(/\s/g,' ');
  8. a=a.split(' ');
  9. for (z=0; z<a.length; z++) {if (a[z].length > 0) r++;}
  10. x.value=r;
  11. }
  12.  
  13. //--></script>
  14.  
  15. <form name="form" action="http://www.xyz.com/sc/cart.cgi" method="post">
  16.  
  17. Ad Content: <textarea onkeyup="cnt(this,document.form.op3)" name="op31"></textarea>
  18.  
  19. Word Count: <input value="0" name="op3" />
  20.  
  21. Ad Cost: <input value="0" name="op4" />
  22.  
  23. <input type="image" src="../../UploadedImages/ShoppingCartButtons/addtocart.gif" name="add" /></p>
  24. </form>
  25.  
I am hoping to take the value of op3 (which is generated from the cnt() function) and use it to determine cost and display that in op4. The cost basis is

If op3 = 1-150, $200
151-300, $350
301-450, $500
451-600, $650
>600 = invalid

Everything function I have tried either makes cnt() not show the word count in op3 or does nothing at all.
May 9 '07 #1
9 6838
iam_clint
1,208 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. <script>
  2. function word_count(val) {
  3. val = val.split(" ");
  4. document.form.op3.value = val.length
  5. update_cost(val.length);
  6. }
  7. function update_cost(val) {
  8. var price = "";
  9. if (val>=1 && val<=150) { price = "$200"; }
  10. if (val>=151 && val<=300) { price = "$350"; }
  11. if (val>=301 && val<=450) { price = "$500"; }
  12. if (val>=451 && val<=600) { price = "$650"; }
  13. if (val>600) { price = "Invalid"; }
  14. document.form.op4.value = price;
  15. }
  16. </script>
  17. <form name="form" action="http://www.xyz.com/sc/cart.cgi" method="post">
  18. Ad Content: <textarea onkeyup="word_count(this.innerHTML)" name="op31"></textarea>
  19. Word Count: <input value="0" name="op3" />
  20. Ad Cost: <input value="0" name="op4" />
  21. <input type="image" src="../../UploadedImages/ShoppingCartButtons/addtocart.gif" name="add" /></p>
  22. </form>
  23.  
try that on for size (its a pretty skirt...) :P
May 9 '07 #2
The price calculation works great! The only thing I see is that if with the new word count function, it counts the spaces, so if you double space a sentence it will count the extra space as another word. I can try to figure that out though.

Thanks so much!!!!!!!! I spent a ton of time on that and got nowhere.
May 9 '07 #3
Already got it. I changed:

val = val.split(" ");
to
val = val.split(/\s+/g);

I'm sure you knew that though.

Thanks again!
May 9 '07 #4
iam_clint
1,208 Expert 1GB
yes but thanks for replying your result sorry I wasn't even thinking about double spaces.. duh! :) glad I could help
May 9 '07 #5
pbmods
5,821 Expert 4TB
*ahem*

Expand|Select|Wrap|Line Numbers
  1. val.match(/\b\w+\b/g).length;
  2.  
May 10 '07 #6
*ahem*

Expand|Select|Wrap|Line Numbers
  1. val.match(/\b\w+\b/g).length;
  2.  
Well, I can't say I know what that means, but I tried putting it in my function and it either made things stop working or started counting characters. What should that be doing differently and if it's better to have it, where should it go? Seems to be working fine without.
May 10 '07 #7
pbmods
5,821 Expert 4TB
Well, I can't say I know what that means, but I tried putting it in my function and it either made things stop working or started counting characters. What should that be doing differently and if it's better to have it, where should it go? Seems to be working fine without.
Expand|Select|Wrap|Line Numbers
  1. val.match(/\b\w+\b/g).length;
  2.  
evaluates to the number of words in val.

Basically, it invokes val.match, which searches for a string within a string. The argument in match is a regular expression which searches for alpha-numeric characters separated by non-alpha-numeric characters. The '/g' part means to return ALL matches in the form of an array.

And then we get the length of the resulting array.

So if you set val to, say "This is a long string with a bunch of words in it.",
Expand|Select|Wrap|Line Numbers
  1. val.match(/\b\w+\b/g).length;
  2.  
would evaluate to 12.
May 10 '07 #8
iam_clint
1,208 Expert 1GB
so let me put it in the code for you

Expand|Select|Wrap|Line Numbers
  1. <script>
  2. function word_count(val) {
  3. var words =  val.match(/\b\w+\b/g).length;
  4. document.form.op3.value = words;
  5. update_cost(words);
  6. }
  7. function update_cost(val) {
  8. var price = "";
  9. if (val>=1 && val<=150) { price = "$200"; }
  10. if (val>=151 && val<=300) { price = "$350"; }
  11. if (val>=301 && val<=450) { price = "$500"; }
  12. if (val>=451 && val<=600) { price = "$650"; }
  13. if (val>600) { price = "Invalid"; }
  14. document.form.op4.value = price;
  15. }
  16. </script>
  17. <form name="form" action="http://www.xyz.com/sc/cart.cgi" method="post">
  18. Ad Content: <textarea onkeyup="word_count(this.innerHTML)" name="op31"></textarea>
  19. Word Count: <input value="0" name="op3" />
  20. Ad Cost: <input value="0" name="op4" />
  21. <input type="image" src="../../UploadedImages/ShoppingCartButtons/addtocart.gif" name="add" /></p>
  22. </form>
  23.  
May 10 '07 #9
pbmods
5,821 Expert 4TB
Shoot. I forgot two things. First off, if you want to learn more about regular expressions, check out this site:

http://www.regular-expressions.info/

I also realized that the code I provided will generate an error if there are zero (and sometimes one) words.

So you have to modify things a little bit.

Using the code provided by iam_clint:

Expand|Select|Wrap|Line Numbers
  1. function word_count(val) {
  2. var words =  val.match(/\b\w+\b/g);
  3.  
  4. words = ((words && (words.constructor == Array))
  5.     ? words.length
  6.     : 1 * Boolean(words)
  7. );
  8.  
  9. document.form.op3.value = words;
  10. update_cost(words);
  11. }
  12.  
If there are zero words, then val.match(/\b\w+\b/g) will evaluate to null (which if converted to a Boolean, resolves to false, which we can turn into 0). If you try to get words.length when words is null, though, you'll get an error on some browsers, and a non-numeric result on others.

If there is only one word, depending on the browser, val.match(/\b\w+\b/g) might evaluate to an array with one element, or it might be a string containing that word. If it is a string, words.length will be the number of characters in that string instead of the number of words. So if words is not an array, we return 1 (we use Boolean(words), which evaluates to true, * 1 to turn it into a number; alternately, you could use Number(Boolean(words)).

If there is more than one word, this code still works as intended.
May 10 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: jochen scheire | last post by:
Is there a way I can calculate a field in a form based on another field in the same form. When clicking submit, both values should be posted to the next page. I want to be able to type in a value...
5
by: Mark 123 | last post by:
Hi I have a form with the input fields: ProductName Quantity and output fields: ProductPrice
9
by: Alex | last post by:
Hi, Is it possible to perform a swicth statement based on a range of values ? eg. switch(Answer) case : 0 - 4 : do something; break;
2
by: Kevin | last post by:
Hi all, Does anyone know of a script or package that will allow me to calculate the localtime given a country code (and optional state/province for US/Canada)? It should factor in daylight...
1
by: areej | last post by:
hi ppl am new in learning C++ n i hvent got any clue abt the range of floating no'z so can any one help me out? how we find range of floating point no'z? regardz areej
3
by: gator6688 | last post by:
I have to write a program that asks for a cost-per-item, number of items purchased, and a discount rate. Then it should calculate the total cost, tax due, and amount due. I have to use the...
12
by: brossyg | last post by:
I have a very simple price times quantity calculation. The input is STWCORQuant: <input name="STWCORQuant" class="contactitalicbold" id="STWCORQuant" onChange="javascript:STWCORPrice();"...
3
by: santoshsri | last post by:
Hi , I have a requirement in which the client's portfolio's net worth should change dynamically based on current price of Gold and silver. Please advise how can I get this done. Thanks !!
19
Breeves22
by: Breeves22 | last post by:
Hi all, I was wondering if it was possible to use Access 2000 to auto calculate a date in a field operating under a 5 day working week Thankyou in advance
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.