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

How Do I keep from getting this "NaN" in blank fields

Status
Not open for further replies.

rampage60

IS-IT--Management
Jul 1, 2002
73
NO
How do I keep from getting the "NaN" in the fields if I don't fill out all of the fields. Thanks!

<html>
<head>
</head>

<body>
<p>Mileage Report</p>
<SCRIPT LANGUAGE=&quot;javascript&quot;>
function add()
{
var sum01 = document.george.start01.value;
var sum02 = document.george.finish01.value;
var sum = parseInt(sum02) - parseInt(sum01);
document.george.total01.value = sum;

var sum03 = document.george.start02.value;
var sum04 = document.george.finish02.value;
var suma = parseInt(sum04) - parseInt(sum03);
document.george.total02.value = suma;

var sum05 = document.george.start03.value;
var sum06 = document.george.finish03.value;
var sumb = parseInt(sum06) - parseInt(sum05);
document.george.total03.value = sumb;

document.george.gross.value = sum + suma + sumb;
}
</script>

<div align=&quot;center&quot;><form name=&quot;george&quot;>


Start <input type=text name=&quot;start01&quot; size=&quot;4&quot;>
Finish <input type=text name=&quot;finish01&quot; size=&quot;4&quot;>
Total <input type=text name=&quot;total01&quot; size=&quot;4&quot; readonly=&quot;&quot;>
<br>
Start <input type=text name=&quot;start02&quot; size=&quot;4&quot;>
Finish <input type=text name=&quot;finish02&quot; size=&quot;4&quot;>
Total <input type=text name=&quot;total02&quot; size=&quot;4&quot; readonly=&quot;&quot;>
<br>
Start <input type=text name=&quot;start03&quot; size=&quot;4&quot;>
Finish <input type=text name=&quot;finish03&quot; size=&quot;4&quot;>
Total <input type=text name=&quot;total03&quot; size=&quot;4&quot; readonly=&quot;&quot;><br>

Gross <input type=text name=&quot;gross&quot; size=&quot;4&quot;><br>

<p><input type=&quot;button&quot; value=&quot;Do It&quot; onClick=&quot;add()&quot;></div></p>
</form>

</body>
</html>
 
loop through the fields and give a value of 0 to them if empty. seeing as you are not validating the fields for a empty value this would be the easy solution
eg:
for (x = 0; x <= MyForm.length; x++) {
if(document.MyForm.elements[x].type==&quot;text&quot;){
//add another condition for further checking what fields to set
document.MyForm.elements[x].value = 0
}

other then that you could just add to your script some validation prior to setting the var's
if(document.george.start01.value==&quot;&quot;) {
document.george.start01.value= 0

or check the vars in the same way.

____________________________________________________
The most important part of your thread is the subject line.
Make it clear and about the topic so we can find it later for reference. Please!! faq333-3811

onpnt2.gif
 
Where you get the values for the calculations, you need to validate what is in the fields before performing numeric operations on them. (By the way 'NaN' stands for Not a Number)

If you don't put a value (or put a text value) in any of your fields, the parseInt() function will return NaN because, obviously, an empty string or a string containing alpha characters is not a number.

To perform some validation you should check the values after getting them out of the fields but before running parseInt() over them. To check for a numeric value you can make use of the isNaN(value) function. isNaN() returns true or false depending on whether the tested value is not, or is a number. You can use the output of isNaN() in an if() statement to validate your form input as in the following example:
[tt]
var sum01 = document.george.start01.value;
if(isNaN(sum01)){
sum01 = 0;
}
var sum02 = document.george.finish01.value;
if(isNaN(sum02)){
sum02 = 0;
}
var sum = parseInt(sum02) - parseInt(sum01);
document.george.total01.value = sum;
[/tt]

This will ensure that the operands are always numbers by the time you try to add them together.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top