View Full Version : Javascript Minimum Quantities - HELP!

09-06-2011, 08:30 AM
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:
<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!


function checkQuantity()
var minimumQuantity = document.getElementById("minimumQuantity").innerHTML;
if(minimumQuantity != "Not applicable")
var quantityEntered = document.getElementById("qty").value;
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;

09-06-2011, 09:19 AM
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.

09-06-2011, 09:44 AM
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/419/78/Love-in-Paris---Eiffel-Tower-Candles/ (or any other product on that site with a minimum quantity number)


09-06-2011, 09:49 AM
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

// this is a subroutine call of parseInt. It will execute but as a final result it won't do anything!

// 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);

09-06-2011, 10:40 AM
That appears to have done it, many thanks!