By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,060 Members | 1,906 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,060 IT Pros & Developers. It's quick & easy.

Calculate Price Based On Quantity Range

P: 11
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
Share this Question
Share on Google+
9 Replies


iam_clint
Expert 100+
P: 1,208
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

P: 11
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

P: 11
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
Expert 100+
P: 1,208
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
Expert 5K+
P: 5,821
*ahem*

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

P: 11
*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
Expert 5K+
P: 5,821
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
Expert 100+
P: 1,208
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
Expert 5K+
P: 5,821
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

Post your reply

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