I have a javascript function that retrieves number values, add them together and then assigns that total to a text field on a form.
The problem i am having is that if the one of the number fields where the function is retreiving data is blank, then NaN appears in the text field.
Is there a way to check for nulls and if so, assign a 0?
The code is -
<!--FUNCTION TO CALCULATE THE TOTAL AMOUNT-->
<script language="javascript">
function calctotal()
{
lvShip = parseFloat(document.forms['ordersnewform'].elements['OrderShipping'].value);
lvMisc = parseFloat(document.forms['ordersnewform'].elements['OrderMisc'].value);
lvUnitPrice1 = parseFloat(document.forms['ordersnewform'].elements['SubOrderUnitPrice1'].value);
lvUnitPrice2 = parseFloat(document.forms['ordersnewform'].elements['SubOrderUnitPrice2'].value);
lvUnitPrice3 = parseFloat(document.forms['ordersnewform'].elements['SubOrderUnitPrice3'].value);
lvUnitPrice4 = parseFloat(document.forms['ordersnewform'].elements['SubOrderUnitPrice4'].value);
lvQuantity1 = parseFloat(document.forms['ordersnewform'].elements['SubOrderQuantity1'].value);
lvQuantity2 = parseFloat(document.forms['ordersnewform'].elements['SubOrderQuantity2'].value);
lvQuantity3 = parseFloat(document.forms['ordersnewform'].elements['SubOrderQuantity3'].value);
lvQuantity4 = parseFloat(document.forms['ordersnewform'].elements['SubOrderQuantity4'].value);
lvUnit1 = lvUnitPrice1 * lvQuantity1
lvUnit2 = lvUnitPrice2 * lvQuantity2
lvUnit3 = lvUnitPrice3 * lvQuantity3
lvUnit4 = lvUnitPrice4 * lvQuantity4
lvTotal = lvShip + lvMisc + lvUnit1 + lvUnit2 + lvUnit3 + lvUnit4;
document.forms['ordersnewform'].elements['OrderTotals'].value = lvTotal;
return true;
}
</script>
Example - I retrieve a value for lvShip, then I check to see if it is null. If so, assign a 0, if not then assign the value it is.
I have tried (to no avail)-
If parseFloat(document.forms['ordersnewform'].elements['OrderShipping'].value) is null
{
lvship = 0;
}
else
{
lvShip = parseFloat(document.forms['ordersnewform'].elements['OrderShipping'].value);
}
The problem i am having is that if the one of the number fields where the function is retreiving data is blank, then NaN appears in the text field.
Is there a way to check for nulls and if so, assign a 0?
The code is -
<!--FUNCTION TO CALCULATE THE TOTAL AMOUNT-->
<script language="javascript">
function calctotal()
{
lvShip = parseFloat(document.forms['ordersnewform'].elements['OrderShipping'].value);
lvMisc = parseFloat(document.forms['ordersnewform'].elements['OrderMisc'].value);
lvUnitPrice1 = parseFloat(document.forms['ordersnewform'].elements['SubOrderUnitPrice1'].value);
lvUnitPrice2 = parseFloat(document.forms['ordersnewform'].elements['SubOrderUnitPrice2'].value);
lvUnitPrice3 = parseFloat(document.forms['ordersnewform'].elements['SubOrderUnitPrice3'].value);
lvUnitPrice4 = parseFloat(document.forms['ordersnewform'].elements['SubOrderUnitPrice4'].value);
lvQuantity1 = parseFloat(document.forms['ordersnewform'].elements['SubOrderQuantity1'].value);
lvQuantity2 = parseFloat(document.forms['ordersnewform'].elements['SubOrderQuantity2'].value);
lvQuantity3 = parseFloat(document.forms['ordersnewform'].elements['SubOrderQuantity3'].value);
lvQuantity4 = parseFloat(document.forms['ordersnewform'].elements['SubOrderQuantity4'].value);
lvUnit1 = lvUnitPrice1 * lvQuantity1
lvUnit2 = lvUnitPrice2 * lvQuantity2
lvUnit3 = lvUnitPrice3 * lvQuantity3
lvUnit4 = lvUnitPrice4 * lvQuantity4
lvTotal = lvShip + lvMisc + lvUnit1 + lvUnit2 + lvUnit3 + lvUnit4;
document.forms['ordersnewform'].elements['OrderTotals'].value = lvTotal;
return true;
}
</script>
Example - I retrieve a value for lvShip, then I check to see if it is null. If so, assign a 0, if not then assign the value it is.
I have tried (to no avail)-
If parseFloat(document.forms['ordersnewform'].elements['OrderShipping'].value) is null
{
lvship = 0;
}
else
{
lvShip = parseFloat(document.forms['ordersnewform'].elements['OrderShipping'].value);
}