Web Analytics Made Easy -
StatCounter Javascript Minimum Quantities - HELP! - CodingForum

Announcement

Collapse
No announcement yet.

Javascript Minimum Quantities - HELP!

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Javascript Minimum Quantities - HELP!

    I have been trying to get this to work for the last few hours with no luck. I'm convinced there's something obvious that I am missing but I just cannot see it!

    For info, the "minimumQuantity" is a <span id> generated by my client's site's eCommerce software, for example:
    Code:
    <span id="minimumQuantity">8</span>
    I've checked that this is the only span with this ID on the product detail page. If there is no minimum quantity, "Not applicable" is generated instead of a number.

    Under the example of a minimum quantity of 8:
    • The error is displayed if you enter 7 or less (including minus numbers) into the quantity field. This is correct.
    • The error is NOT displayed if you enter 8 or 9 into the quantity field. This is correct.
    • The error IS displayed if you enter between 10-79 in the quantity field. This is NOT correct.
    • The error is NOT displayed if you enter between 80-99 in the quantity field. This IS correct, but makes no sense!


    Code:
    Code:
    function checkQuantity()
    {
     var minimumQuantity = document.getElementById("minimumQuantity").innerHTML;
     if(minimumQuantity != "Not applicable")
     {
      var quantityEntered = document.getElementById("qty").value;
      parseInt(minimumQuantity);
      parseInt(quantityEntered);
      if(quantityEntered < minimumQuantity)
      {
       alert("The minimum order for this product is "+minimumQuantity+" units.\n\nYou entered "+quantityEntered+". Please correct the quantity and try again.");
       return false;
      }
     }
    }

  • #2
    parseInt is a function and will return(!) the converted value. It will not convert the parameter in itself. You did not assign the return value to anything, so it is lost and minimumQuantity will still be a string.

    Comment


    • #3
      Devnull69, thanks for your fast response.

      I added the parseInt() function in as a last resort, but even changing them to Number(minimumQuantity); and Number(quantityEntered), or even removing it entirely, produces the same result.

      I think the error is elsewhere in the code.

      You can see a working example at http://www.askusafavour.co.uk/shop/4...Tower-Candles/ (or any other product on that site with a minimum quantity number)

      Stuart

      Comment


      • #4
        Can you please post the updated version of the function checkQuantity() ?

        EDIT: Ok, I see ... you are still using parseInt() or Number() as subroutines rather than as functions

        Code:
        // this is a subroutine call of parseInt. It will execute but as a final result it won't do anything!
        parseInt(minimumQuantity);
        
        // this is a correct function call for parseInt, it will execute the function and return an Integer (if possible). This will be assigned to minimumQuantity again
        // only then minimumQuantity will become a number
        // the second parameter will make sure that the result is provided to the base of 10
        minimumQuantity = parseInt(minimumQuantity, 10);
        Last edited by devnull69; Sep 6, 2011, 04:53 AM.

        Comment


        • #5
          That appears to have done it, many thanks!

          Comment

          Working...
          X
          😀
          🥰
          🤢
          😎
          😡
          👍
          👎