PDA

View Full Version : Numeric value acting as a string



Mar 26th, 2007, 02:00 AM
When I try to get the two input fields to add, instead of adding the to values together and displaying the sum in the last feild, it simply put the to values together.

example: 1 + 2 = 12

what i want is: 1 + 2 = 3

Please help. i can't figure out what I am doing wrong.

<code>
<script Language=JavaScript>
function income_total(form)
{
var total;
total = eval("form.income_dollar1.value + form.income_dollar2.value");
form.total_income.value = total;


}
</script>



<FIELDSET>
<p align="center"><input type="text" name="income_dollar1" size="12" onKeyPress="return numbersonly(this, event)" onblur="income_total(this.form)" ><br>
<p align="center"><input type="text" name="income_dollar2" size="12" onKeyPress="return numbersonly(this, event)" onblur="income_total(this.form)"></p>


<input type="text" name="total_income" size="12" onblur="income_total(this.form)">
</FIELDSET>
</code>

_Aerospace_Eng_
Mar 26th, 2007, 02:12 AM
Don't use eval. Its slow and not necessary. You should use the javascript Number(form.income_dollar2.value) function. Your script treats the numbers as a string because they are strings since they come from a text box.

Also your HTML is quite bad. I suggest doing some HTML tutorials before you try to do any more javascript.

Mar 26th, 2007, 02:33 AM
Thank you. That worked. BTW, I am not building this websight for money. I am building it for myself because I don't like any of the financial software that is out there, and my websight is looking quite good even though my HTML is terrible. Thankyou again.