...

View Full Version : Need to create a form that calculates 3 fields...



viper21
01-26-2006, 10:16 PM
I want to have two fields. You put in a number (ie 10) then in the next field you put another number (ie 15) then it multiplies those numbers together, and then mulitplies the result by .75

anything out there already? or something that can be modified? i'm not a JS expert, so some assistance might be needed if we can find something that will work..'

Thanks!

Lerura
01-26-2006, 10:54 PM
javascript will allways try to convert strings to numbers when multiplying and dividing.
so in this case you can just say:
Result = field1.value * field2.value * .75;

whereas if you want to add or substract,
you will have to convert the strings to numbers before the calculation.
It can be done by multiplying the strings by 1 .e.g.:
Result = (field1.value * 1) + (field2.value * 1);.

need more help? just ask!

viper21
01-27-2006, 05:44 AM
Thanks, but i'm a total noob at JS... I can't really figure it out by the line of code you gave...

Could you use it in an example form?

Thanks a million!! :)

viper21
01-27-2006, 06:23 AM
Okay, nevermind. I figured it out!! Thanks!!

arnyinc
01-27-2006, 06:25 AM
<html>
<head>
<script language="javascript">

function doMath(valueA, valueB, multiplier){
return (valueA*valueB*multiplier);
}

</script>
</head>
<body>

<form onsubmit="alert(doMath(this.boxA.value, this.boxB.value, .75));">
<input type="text" name="boxA"><br>
<input type="text" name="boxB"><br>
<input type="submit">
</form>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum