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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking For a Null Value 1

Status
Not open for further replies.

FontanaS

Programmer
May 1, 2001
357
US
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 values in your fields are treated as text values.
When you perform a calculation on a field that contains only numerical data the data will automatically cast to a number but when you have a field that is empty it is obviously not a number so the calculation cannot occur.

An empty field value is not actually null, it just has no value. Null is an entirely different thing and you do not need to worry about it in this case.

You just need to make certain that your values are treated as numbers so you could just multiply each value by 1 when you read the value from the field, or subtract 0 from the number. For instance:
Code:
lvShip = parseFloat(document.forms['ordersnewform'].elements['OrderShipping'].value[COLOR=red]-1[/color]);

But, people could type in other non-numerical characters which would cause the calculation to fail. If this is an issue for you then you may want to create a function to test if each value is indeed a number before performing the calculation.


At my age I still learn something new every day, but I forget two others.
 
Here is a function I have used to test that values are numerical.
Code:
function isNum(num,which,min,max)
{
  switch (which) {
    case 1: re = /^\d+$/; break;  //whole positive number
    case 2: re = /^-?\d+$/; break;  //Any whole number
    case 3: re = /^-?\d+\.\d+$/; break;  //Any real number with decimal point:
    case 4: re = /^-?\d+(\.\d+)?$/; break;  //Any real number with or without decimal part:
    case 5: re = new RegExp("^-?\\d+(\\.\\d{" + min + "," + max + "})?$");  break; //Any real number with set number of decimal places.
  }
  return re.test(num);
}

You can pass in the value, which test to perform and min/max values.
I set it up this way so the same function could be used to test different types of numbers. 1=Whole positive number, 2=any whole number, 3=any real number, 4=real number with decimal point, 5=real number with fixed length decimal places.

The min and max values are really only for test 5 when you want to make certain that the value is a real number and you want to specify how many decimal places the number has to be or a range. For instance you could make a call like this
isNum(5,myvalue,3,5)
which would mean use test 5 to see if myvalue is a real number with between 3 to 5 decimal places.


At my age I still learn something new every day, but I forget two others.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top