How to compare values in JavaScript for minimum order quantity
Ok, basically I have been given the task of producing a payment page for us. I'm having a little problem. My boss wants me to integrate google checkout, and I have that working fine. He also wants me to define an order minimum.
For example: we sell labels at "£11.00" per "1000", but the minimum invoice is "£33.00"
I have setup my page with 4 boxes. One is qty (Quantity), another is cost (Price) and two hidden fields. One of these fields is for google checkout (the product prices) the final field is a hidden field I put there, to try to compare the prices and update the cost.
Currently, I have on my quantity change textbox onChange event, this:
function labels_1 () {
var cost;
var costper1k = '11.00';
var qty = orderform.labels_1_qty.value;
if ( isNaN( qty ) ) {
orderform.labels_1_cost.value = "Not a Valid Quantity";
}
else if ( qty < 0 ) {
orderform.labels_1_cost.value = "Negative Quantity";
}
else {
cost = ((qty/1000)*costper1k);
}
orderform.labels_1_cost.value = formatCurrency(cost);
orderform.hiddenprice_1.value = cost;
orderform.prodprice_1.value = orderform.labels_1_cost.value;
orderform.productname_1.value='26x44mm (4a) 76mm Cores'+' x '+orderform.labels_1_qty.value;
}
This is working well, without the validation of minimum invoice amount.
I have this 6 functions, all like this because we sell 6 label types. As I said, google checkout works fine with all these.
I tried creating this:
function hidden_1 () {
var cost = '33.00';
var originalcost = orderform.labels_1_cost.value;
var a = Number(orderform.hidden_1.value);
var b = Number(orderform.hidden_2.value);
var c = Number(orderform.hidden_3.value);
var d = Number(orderform.hidden_4.value);
var e = Number(orderform.hidden_5.value);
var f = Number(orderform.hidden_6.value);
var total = a+b+c+d+e+f;
if (total.value < '33'){
orderform.labels_1_cost.value = formatCurrency(cost);}
if (total.value > '33') {
orderform.labels_1_cost.value = originalcost;}
}
and appending hidden_1(); to the end of the label_1() function.
It doesn't work, And I am tearing my hair out trying to fix it. I'm not even sure I am doing that right.
Can Anyone offer any help? many thanks
edit
Sorry, here is my formatCurrency function:
function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '£' + num + '.' + cents);
}
Full Site Code: Order.php:
<html>
<head>
<style type="text/css" media="screen">@import "css/forms-style.css";</style>
<script type="text/javascript" src="js/labels.js"></script>
</head>
<script id="googlecart-script" type="text/javascript"
src="http://checkout.google.com/seller/gsc/v2/cart.js?mid=REMOVED!!!"
currency="GBP"
highlight-time="5000"
highlight-color="#FF7878"
cart-opening-time="500"
hide-cart-when-empty="true"
close-cart-when-click-away="false"
post-cart-to-sandbox="true">
</script>
<body>
<div id="wrapper">
<form name="orderform">
<!-- ----------------------------------------------------------------------- -->
<div id="content">
<div class="heading-section">
<h2>Printed Labels</h2>
<p>All Printed Labels come outside wound, with a standard <strong>10 item setups per roll.</strong></p>
<p>The minimum Invoice amount is <strong>£30.00</strong></p>
</div>
<div align="center" id="statement">Hover Over Images To Zoom</div>
<br/>
<p>
<?php tableformat( '1', '26x44mm (4a) 76mm Cores','26x44mm Labels. Custom options are available for these labels. They can include the text SPECIAL BUY or SPECIAL PURCHASE overprinted in <font color=red><strong>RED</strong></font> ink. These labels can also include a Country of Origin and the John lewis Address. The labels are also available in blockout (opaque) backing, to be used for repricing, or generally covering other labels.','Style: <select class="product-attr-color">
<option>Normal</option>
<option>Special Buy</option>
<option>Special Purchase</option>
<option>Opaque Backing Material</option>
</select>','img/img_1.png','img/img_7.png','img/img_8.png','img/img_1_thumb.png','img/img_7_thumb.png','img/img_8_thumb.png' ); ?>
<?php tableformat( '2', '44x26mm (2a) 76mm Cores','44x26mm Labels. Custom options are available for these labels. They can include the text SPECIAL BUY or SPECIAL PURCHASE overprinted in <开发者_JS百科font color=red><strong>RED</strong></font> ink. These labels can also include a Country of Origin and the John lewis Address. The labels are also available in blockout (opaque) backing, to be used for repricing, or generally covering other labels.','Style: <select class="product-attr-color">
<option>Normal</option>
<option>Special Buy</option>
<option>Special Purchase</option>
<option>Opaque Backing Material</option>
</select>','img/img_2.png','','','img/img_2_thumb.png','','' ); ?>
<?php tableformat( '3', '35x13mm (2a) 76mm Cores','35x13mm Labels. These Labels can include a Country of Origin. Please Note, the size of the label is fairly limited','','img/img_3.png','','','img/img_3_thumb.png','','' ); ?>
<?php tableformat( '4', '80x10mm [dumbells] (1a) 76mm Cores','80x10mm Dumbell Labels. These Labels can be produced with either an EAN8 or an EAN13 consumer code','','img/img_4.png','','','img/img_4_thumb.png','','' ); ?>
<?php tableformat( '5', '25x10mm [price only] (4a) 76mm Cores','25x10mm Labels. These Small labels may include a Price Only. No text will be printed on these labels. If you require Text, please look at some of the other sizes above','','img/img_5.png','','','img/img_5_thumb.png','','' ); ?>
<?php tableformat( '6', '15x8mm [price only] (4a) 76mm Cores','15x8mm Labels. These Small labels may include a Price Only. No text will be printed on these labels. If you require Text, please look at some of the other sizes above','','img/img_6.png','','','img/img_6_thumb.png','','' ); ?>
</p>
<div class="heading-section" align="right">
<input type="reset">
<!-- ----------------------------------------------------------------------- -->
</hr>
</h2>
</div>
</div>
</form>
<br/><br/>
</div>
</div>
</body>
</html>
<?php
function tableformat( $num, $label, $labeldesc, $options, $pic1, $pic2, $pic3, $thumb1, $thumb2, $thumb3) {
echo
'<div id="'.$num.'" class="section">
<div class="product">
<table border=0 width=100% align=center class="product-table">
<tr valign=top bgcolor=3366cc>
<td colspan=4><span class="product-name">'.$label.'</span> <a class="p1" href="#v"><img onerror="this.style.display=\'none\'" src="'.$thumb1.'" alt=""><b><img class="large" onerror="this.style.display=\'none\'" src="'.$pic1.'" alt=""></b></a> <a class="p1" onerror="this.style.display=\'none\'" ;href="#v"><img onerror="this.style.display=\'none\'" src="'.$thumb2.'" alt=""><b><img onerror="this.style.display=\'none\'" class="large" src="'.$pic2.'" alt=""></b></a> <a class="p1" href="#v"><img onerror="this.style.display=\'none\'" src="'.$thumb3.'" alt=""><b><img class="large" onerror="this.style.display=\'none\'" src="'.$pic3.'" alt=""></b></a></td>
<input type="hidden" name="productname_'.$num.'" class="product-title" value="0"/>
</tr>
<tr><td> </td>
</tr>
<tr>
<td align="left" colspan="2">
<span id="labels_'.$num.'_exp">'.$labeldesc.'</span>
</td>
</tr>
<tr><td> </td>
</tr>
<tr valign=center align=right>
<td width=30%>
</td>
<td width=auto>
'.$options.'
<strong>Quantity:</strong> <input type="text" name="labels_'.$num.'_qty" size="10" value=0 onChange="OnChan();">
<strong>Price:</strong> <input type="text" name="labels_'.$num.'_cost" class="form-cost" size="10" value="0.00" onChange="OnChan();">
<input type="hidden" name="prodprice_'.$num.'" class="product-price" value="£0.00"/>
<input type="hidden" name="hiddenprice_'.$num.'" value="0.00"/>
</td>
</tr>
<tr>
<td> </td>
</tr>
<td width=30%>
</td>
<td width=auto align=right>
<div role="button" alt="Add to cart" tabindex="0" class="googlecart-add-button" onClick="hidden_'.$num.';">
</div>
</td>
</tr>
</table>
<br/>
<br/>
</div></div>';
} // tableformat
?>
labels.js is here:
function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '£' + num + '.' + cents);
}
function isPositiveInteger(val){
if(val==null){return false;}
if (val.length==0){return false;}
for (var i = 0; i < val.length; i++) {
var ch = val.charAt(i)
if (ch < "0" || ch > "9") {
return false
}
}
return true;
}
/* -----------------------------------------------------------------------------
LABELS - 1
----------------------------------------------------------------------------- */
function labels_1 () {
var cost, desc;
var costper1k = '11.00';
var qty = orderform.labels_1_qty.value;
if ( isNaN( qty ) ) {
orderform.labels_1_cost.value = "Not a Valid Quantity";
desc = 'Not a Valid Quantity';
}
else if ( qty < 0 ) {
orderform.labels_1_cost.value = "Negative Quantity";
desc = 'Not a Valid Quantity';
}
else {
cost = ((qty/1000)*costper1k);
desc = qty+' ordered at a cost of '+formatCurrency(cost);
}
orderform.labels_1_cost.value = formatCurrency(cost);
alert("test - end of normal");
hidden_1();
}
/* -----------------------------------------------------------------------------
Hidden - 1
----------------------------------------------------------------------------- */
function hidden_1 () {
alert("starting");
var costed = '33.00';
var originalcost = orderform.labels_1_cost.value;
var a = Number(orderform.hidden_1.value);
var b = Number(orderform.hidden_2.value);
var c = Number(orderform.hidden_3.value);
var d = Number(orderform.hidden_4.value);
var e = Number(orderform.hidden_5.value);
var f = Number(orderform.hidden_6.value);
var total = a+b+c+d+e+f;
if (total < 33){
orderform.labels_1_cost.value = costed;}
if (total => 33) {
orderform.labels_1_cost.value = originalcost;}
alert("ending");
}
/* -----------------------------------------------------------------------------
LABELS - 2
----------------------------------------------------------------------------- */
function labels_2 () {
var cost, desc;
var costper1k = '11.00';
var qty = orderform.labels_2_qty.value;
if ( isNaN( qty ) ) {
orderform.labels_2_cost.value = "Not a Valid Quantity";
desc = 'Not a Valid Quantity';
}
else if ( qty < 0 ) {
orderform.labels_2_cost.value = "Negative Quantity";
desc = 'Not a Valid Quantity';
}
else {
cost = ((qty/1000)*costper1k);
desc = qty+' ordered at a cost of '+formatCurrency(cost);
}
orderform.labels_2_cost.value = formatCurrency(cost);
}
/* -----------------------------------------------------------------------------
LABELS - 3
----------------------------------------------------------------------------- */
function labels_3 () {
var cost, desc;
var costper1k = '11.00';
var qty = orderform.labels_3_qty.value;
if ( isNaN( qty ) ) {
orderform.labels_3_cost.value = "Not a Valid Quantity";
desc = 'Not a Valid Quantity';
}
else if ( qty < 0 ) {
orderform.labels_3_cost.value = "Negative Quantity";
desc = 'Not a Valid Quantity';
}
else{
cost = ((qty/1000)*costper1k);
desc = qty+' ordered at a cost of '+formatCurrency(cost);
}
orderform.labels_3_cost.value = formatCurrency(cost);
}
/* -----------------------------------------------------------------------------
LABELS - 4
----------------------------------------------------------------------------- */
function labels_4 () {
var cost, desc;
var costper1k = '13.75';
var qty = orderform.labels_4_qty.value;
if ( isNaN( qty ) ) {
orderform.labels_4_cost.value = "Not a Valid Quantity";
desc = 'Not a Valid Quantity';
}
else if ( qty < 0 ) {
orderform.labels_4_cost.value = "Negative Quantity";
desc = 'Not a Valid Quantity';
}
else {
cost = ((qty/1000)*costper1k);
desc = qty+' ordered at a cost of '+formatCurrency(cost);
}
orderform.labels_4_cost.value = formatCurrency(cost);
}
/* -----------------------------------------------------------------------------
LABELS - 5
----------------------------------------------------------------------------- */
function labels_5 () {
var cost, desc;
var costper1k = '7.89';
var qty = orderform.labels_5_qty.value;
if ( isNaN( qty ) ) {
orderform.labels_5_cost.value = "Not a Valid Quantity";
desc = 'Not a Valid Quantity';
}
else if ( qty < 0 ) {
orderform.labels_5_cost.value = "Negative Quantity";
desc = 'Not a Valid Quantity';
}
else {
cost = ((qty/1000)*costper1k);
desc = qty+' ordered at a cost of '+formatCurrency(cost);
}
orderform.labels_5_cost.value = formatCurrency(cost);
}
/* -----------------------------------------------------------------------------
LABELS - 6
----------------------------------------------------------------------------- */
function labels_6 () {
var cost, desc;
var costper1k = '10.14';
var qty = orderform.labels_6_qty.value;
if ( isNaN( qty ) ) {
orderform.labels_6_cost.value = "Not a Valid Quantity";
desc = 'Not a Valid Quantity';
}
else if ( qty < 0 ) {
orderform.labels_6_cost.value = "Negative Quantity";
desc = 'Not a Valid Quantity';
}
else {
cost = ((qty/1000)*costper1k);
desc = qty+' ordered at a cost of '+formatCurrency(cost);
}
orderform.labels_6_cost.value = formatCurrency(cost);
}
/* -----------------------------------------------------------------------------
OnChan (Changes)
----------------------------------------------------------------------------- */
function OnChan () {
labels_1();
labels_2();
labels_3();
labels_4();
labels_5();
labels_6();
orderform.prodprice_1.value=orderform.labels_1_cost.value;
orderform.prodprice_2.value=orderform.labels_2_cost.value;
orderform.prodprice_3.value=orderform.labels_3_cost.value;
orderform.prodprice_4.value=orderform.labels_4_cost.value;
orderform.prodprice_5.value=orderform.labels_5_cost.value;
orderform.prodprice_6.value=orderform.labels_6_cost.value;
orderform.productname_1.value='26x44mm (4a) 76mm Cores'+' x '+orderform.labels_1_qty.value;
orderform.productname_2.value='44x26mm (2a) 76mm Cores'+' x '+orderform.labels_2_qty.value;
orderform.productname_3.value='35x13mm (2a) 76mm Cores'+' x '+orderform.labels_3_qty.value;
orderform.productname_4.value='80x10mm [dumbells] (1a) 76mm Cores'+' x '+orderform.labels_4_qty.value;
orderform.productname_5.value='25x10mm [price only] (4a) 76mm Cores'+' x '+orderform.labels_5_qty.value;
orderform.productname_6.value='15x8mm [price only] (4a) 76mm Cores'+' x '+orderform.labels_6_qty.value;
}
and a test website is up at:
http://mrotacon.heliohost.org/testing/order.php
also - here is a test version without the comparison, which works fine... but i need the minimum invoice amount
http://mrotacon.heliohost.org/testing/orderworking.php
You don't say how it's not working and what diagnostics you've got but... might it be because you're comparing strings rather than numeric values? So, for example, '6' > '33'. That "total.value" looks dodgy to me too.
精彩评论