Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 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
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    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; 09-06-2011 at 09:53 AM.

  • #5
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That appears to have done it, many thanks!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •