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

NaN Problem 1

Status
Not open for further replies.

cdw0308

Technical User
Oct 8, 2003
181
0
0
US
I am tring to calculate a field on a form by mutiplying two other fields together. I keep getting NaN as the value.
I am using the on blur event on x_proj_value and x_proj_percent so that it populates a value into x_ext_value.
Here is what I have so far.

Code:
<script language="javascript" type="text/javascript">
function calculateestvalue(){
	var1=document.mainadd.x_proj_value;
	var2=document.mainadd.x_proj_percent;
	var1 = (var1=="")?0:var1;
	var2 = (var2=="")?0:var2;
	var3= var1*var2;
	document.mainadd.x_est_value.value=var3;
}
</script>

<cfform onSubmit="return EW_checkMyForm(this);"  name="mainadd" action="mainadd.cfm" method="post">
 <input type="text"  name="x_est_value"/>
<table class="ewTable">
<tr>
		<td class="ewTableHeader">Proj Value&nbsp;</td>
		<td class="ewTableAltRow"><input type="text" name="x_proj_value" size="30" maxlength="50" value="#HTMLEditFormat(x_proj_value)#" onblur="calculateestvalue()">&nbsp;</td>
</tr>
<tr>
		<td class="ewTableHeader">Proj Percent&nbsp;</td>
		<td class="ewTableAltRow"><cfif x_proj_percent EQ ""><cfset x_proj_percent = 0></cfif><cfselect name="x_proj_percent" query="get_proj_percent" display="proj_percent" width="100" value="proj_percent" onblur="calculateestvalue()" ></cfselect>&nbsp;</td>
</tr>
</table>
</cfform>
 
you can use alert field on each field value to see if all your fields are valid numbers, thus finding the offending field.
 
Rock-solid construction may need many more lines than this, but it gives you the start.

[tt]function calculateestvalue(){
var var1,var2,var3;
var1=document.mainadd.x_proj_value.value;
var2=document.mainadd.x_proj_percent.value;
var1 = (parseFloat(var1))?parseFloat(var1):0;
var2 = (parseFloat(var2))?parseFloat(var2):0;
var3= var1*var2;
document.mainadd.x_est_value.value=var3;
}[/tt]

Main difficulty lies in the full validation of acceptable form of number. parseFloat() only give you partial satisfaction. Its defects are:
[1] parseFloat("123abc") gives you "accepted" 123 in value;
[2] parseFloat("1,234.00") gives you 1.
 
Don't you need to turn the field value (a string) into a number? Use parseInt or parseFloat.

_________________
Bob Rashkin
 
I put in
alert(var1);
alert(var2);

and it returns: [object]
for both alert boxes.

Any suggestions?
 
Thanks tsuji

That worked great.
I have it working correctly now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top