Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Making Sure User Entered Number - Put 0 If A Letter 1

Status
Not open for further replies.

FontanaS

Programmer
May 1, 2001
357
US
Hi! I have the following function that takes the fields and does calculations with them to get a total. The problem is that someone put in a word instead of a number and the program errored out. I would like to check for a number. If it is a number - do calculations, if not a number - put a zero in and do calculations.

THANKS~ Any help is greatly appreciated!

<!--FUNCTION TO CALCULATE THE TOTAL AMOUNT-->
<script language="javascript">
function calctotal()
{
lvShip = parseFloat(document.forms['ordersnewform'].elements['OrderShipping'].value-0);
lvShip = parseFloat(document.forms['ordersnewform'].elements['OrderShipping'].value-0);
lvMisc = parseFloat(document.forms['ordersnewform'].elements['OrderMisc'].value-0);
lvUnitPrice1 = parseFloat(document.forms['ordersnewform'].elements['SubOrderUnitPrice1'].value-0);
lvUnitPrice2 = parseFloat(document.forms['ordersnewform'].elements['SubOrderUnitPrice2'].value-0);
lvUnitPrice3 = parseFloat(document.forms['ordersnewform'].elements['SubOrderUnitPrice3'].value-0);
lvUnitPrice4 = parseFloat(document.forms['ordersnewform'].elements['SubOrderUnitPrice4'].value-0);
lvUnitPrice5 = parseFloat(document.forms['ordersnewform'].elements['SubOrderUnitPrice5'].value-0);
lvUnitPrice6 = parseFloat(document.forms['ordersnewform'].elements['SubOrderUnitPrice6'].value-0);
lvQuantity1 = parseFloat(document.forms['ordersnewform'].elements['SubOrderQuantity1'].value-0);
lvQuantity2 = parseFloat(document.forms['ordersnewform'].elements['SubOrderQuantity2'].value-0);
lvQuantity3 = parseFloat(document.forms['ordersnewform'].elements['SubOrderQuantity3'].value-0);
lvQuantity4 = parseFloat(document.forms['ordersnewform'].elements['SubOrderQuantity4'].value-0);
lvQuantity5 = parseFloat(document.forms['ordersnewform'].elements['SubOrderQuantity5'].value-0);
lvQuantity6 = parseFloat(document.forms['ordersnewform'].elements['SubOrderQuantity6'].value-0);

lvUnit1 = lvUnitPrice1 * lvQuantity1
lvUnit2 = lvUnitPrice2 * lvQuantity2
lvUnit3 = lvUnitPrice3 * lvQuantity3
lvUnit4 = lvUnitPrice4 * lvQuantity4
lvUnit5 = lvUnitPrice5 * lvQuantity5
lvUnit6 = lvUnitPrice6 * lvQuantity6

lvTotal = lvShip + lvMisc + lvUnit1 + lvUnit2 + lvUnit3 + lvUnit4 + lvUnit5 + lvUnit6;
document.forms['ordersnewform'].elements['OrderTotals'].value = lvTotal.toFixed(2);
return true;
}
</script>
 
I'd recommend something like this:
Code:
function checkvalue(onevalue)
{
if (onevalue.length == 0)
  {
  return 0;
  }
else if (isNaN(onevalue))
  {
  return 0;
  }
else
  {
  return (parseFloat(onevalue, 10));
  }
}

function calctotal()
{
var el=document.forms['ordersnewform'].elements;

var lvShip = checkvalue(el['OrderShipping'].value);
var lvShip = checkvalue(el['OrderShipping'].value);
var lvMisc = checkvalue(el['OrderMisc'].value);
var lvUnitPrice1 = checkvalue(el['SubOrderUnitPrice1'].value);
var lvUnitPrice2 = checkvalue(el['SubOrderUnitPrice2'].value);
var lvUnitPrice3 = checkvalue(el['SubOrderUnitPrice3'].value);
var lvUnitPrice4 = checkvalue(el['SubOrderUnitPrice4'].value);
var lvUnitPrice5 = checkvalue(el['SubOrderUnitPrice5'].value);
var lvUnitPrice6 = checkvalue(el['SubOrderUnitPrice6'].value);
var lvQuantity1 = checkvalue(el['SubOrderQuantity1'].value);
var lvQuantity2 = checkvalue(el['SubOrderQuantity2'].value);
var lvQuantity3 = checkvalue(el['SubOrderQuantity3'].value);
var lvQuantity4 = checkvalue(el['SubOrderQuantity4'].value);
var lvQuantity5 = checkvalue(el['SubOrderQuantity5'].value);
var lvQuantity6 = checkvalue(el['SubOrderQuantity6'].value);

var lvUnit1 = lvUnitPrice1 * lvQuantity1;
var lvUnit2 = lvUnitPrice2 * lvQuantity2;
var lvUnit3 = lvUnitPrice3 * lvQuantity3;
var lvUnit4 = lvUnitPrice4 * lvQuantity4;
var lvUnit5 = lvUnitPrice5 * lvQuantity5;
var lvUnit6 = lvUnitPrice6 * lvQuantity6;

var lvTotal = lvShip + lvMisc + lvUnit1 + lvUnit2 + lvUnit3 + lvUnit4 + lvUnit5 + lvUnit6;
el['OrderTotals'].value = lvTotal.toFixed(2);
return true;
}

This first assigns the array of form elements to a separate variable to speed up checking, as well as saving typing if you add or modify element names.

By using a separate function to check the value of a form element and return the appropriate number you can modify what you're checking for in one spot.

Lee
 
FontanaS, use the function I've posted below. Instead of calling the parseFloat function in each of your variable declarations, instead call this function. It will return 0 if it is not a valid number, or a parseFloated version of the string if it is a valid number:
Code:
<html>
<head>
<script type="text/javascript">

function blah(str) {
   return (/^(\d+|\d*\.\d+)$/.test(str)) ? parseFloat(str) : 0;
}

</script>
</head>

<body>

<input type="text" id="txt" />
<input type="button" value="test if number" onclick="alert(blah(document.getElementById('txt').value))" />
</body>
</html>

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top