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

How to auto subtract two text values

Status
Not open for further replies.

jrbob34

Technical User
Mar 16, 2011
3
ZA
Hello Everybody

I have a problem with a javascript and i hope someone will be able to help me. I am trying to change the script below from adding the values to subtracting without much luck.


<script type="text/javascript" language="javascript">

function autocalc(oText)
{
if (isNaN(oText.value)) //filter input
{
alert('Numbers only!');
oText.value = '';
}
var field, val, oForm = oText.form, netincome = a = 0;
for (a; a < arguments.length; ++a) //loop through text elements
{
field = arguments[a];
val = parseFloat(field.value); //get value
if (!isNaN(val)) //number?
{
netincome += val; //accumulate
}
}
oForm.netincome.value = netincome; //out
}


</script>


Someone please help
 
Hi

[ul]
[li]How is autocalc() called ?[/li]
[li]What is its oText parameter ?[/li]
[li]How your [tt]form[/tt] looks like ?[/li]
[li]What have you tried so far ?[/li]
[/ul]
Please post your HTML too, do not expect from us to reproduce your document. We gladly help, but please make our task easier by providing all needed information.

Next time please post your code between [tt][ignore]
Code:
[/ignore][/tt] and [tt][ignore]
[/ignore][/tt] TGML tags.

Feherke.
 
Ok, my apologies for the way i posted the code. I am using it with RSForm! Pro in joomla. I have two text fields total_before_deductions and total_expenses then another, netincome, with the auto result.

the source code for the fields is as below:
Code:
<tr>
					<td>Total Monthly Income </td>
					<td><input type="text" value="" size="30"  name="form[total_before_deductions]" id="total_before_deductions" onKeyUp="return autocalc(this,total_expenses)"/><br/>
					<span id="component111" class="formNoError">Invalid Input</span></td>
					<td></td>
				</tr>

				
				<tr>
					<td>Total Monthly Expenses (*)</td>
					<td><input type="text" value="" size="30"  name="form[total_expenses]" id="total_expenses" onKeyUp="return autocalc(this,total_before_deductions)"
/><br/>
					<span id="component112" class="formNoError">Invalid Input</span></td>
					<td></td>
				</tr>
				<tr>
					<td>Net Monthly Income</td>

					<td><input type="text" value="" size="30"  name="form[netincome]" id="netincome" readonly="readonly" value="0" tabindex="-1"/><br/>
 
Hi

Why so generic ? Subtraction's operands are not commutative, so the function's original concept will not work.

And note that an input can be filled with mouse or auto filled by the browser or an extension, which will generate no [tt]onkeyup[/tt] event.

I would do it like this :
JavaScript:
[b]function[/b] [COLOR=darkgoldenrod]autocalc[/color][teal]([/teal]form[teal])[/teal]
[teal]{[/teal]
  [b]var[/b] income[teal]=[/teal][COLOR=darkgoldenrod]parseFloat[/color][teal]([/teal]form[teal].[/teal]total_before_deductions[teal].[/teal]value[teal])-[/teal][COLOR=darkgoldenrod]parseFloat[/color][teal]([/teal]form[teal].[/teal]total_expenses[teal].[/teal]value[teal])[/teal]
  form[teal].[/teal]netincome[teal].[/teal]value[teal]=[/teal][COLOR=darkgoldenrod]isNaN[/color][teal]([/teal]income[teal])?[/teal][purple]0[/purple][teal]:[/teal]income
[teal]}[/teal]
HTML:
[b]<form[/b] [maroon]action[/maroon][teal]=[/teal][green][i]""[/i][/green][b]>[/b]
[b]<input[/b] [maroon]type[/maroon][teal]=[/teal][green][i]"text"[/i][/green] [maroon]value[/maroon][teal]=[/teal][green][i]""[/i][/green] [maroon]name[/maroon][teal]=[/teal][green][i]"total_before_deductions"[/i][/green] [maroon]onkeyup[/maroon][teal]=[/teal][green][i]"autocalc(this.form)"[/i][/green] [maroon]onchange[/maroon][teal]=[/teal][green][i]"autocalc(this.form)"[/i][/green][b]>[/b]
[b]<input[/b] [maroon]type[/maroon][teal]=[/teal][green][i]"text"[/i][/green] [maroon]value[/maroon][teal]=[/teal][green][i]""[/i][/green] [maroon]name[/maroon][teal]=[/teal][green][i]"total_expenses"[/i][/green] [maroon]onkeyup[/maroon][teal]=[/teal][green][i]"autocalc(this.form)"[/i][/green] [maroon]onchange[/maroon][teal]=[/teal][green][i]"autocalc(this.form)"[/i][/green][b]>[/b]
[b]<input[/b] [maroon]type[/maroon][teal]=[/teal][green][i]"text"[/i][/green] [maroon]value[/maroon][teal]=[/teal][green][i]""[/i][/green] [maroon]name[/maroon][teal]=[/teal][green][i]"netincome"[/i][/green] [maroon]readonly[/maroon][teal]=[/teal][green][i]"readonly"[/i][/green] [maroon]value[/maroon][teal]=[/teal][green][i]"0"[/i][/green][b]>[/b]
[b]</form>[/b]


Feherke.
 
Thank a lot feherke ... the solution you provided is on point
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top