Thread: Need help with a auto calculating form

1. Junior Coders
Join Date
Mar 2012
Posts
8
Thanks
2
Thanked 0 Times in 0 Posts

Need help with a auto calculating form

The image below is the way my client needs the calculating form to work.

Every field has to be times the other field to get a total in the last
one, example:
Amount of days x Amount of taxis x Price per taxi per day x 100% minus '% off' contract A, B, C, D = TOTAL
22 x 50 x 15 x 0.93 = R 15,345.00

I am new to this, I have no idea how to work a round this.

Any help will be appreciated.

2. The code itself is very simple.
You simply get the elements by id (`getElementByID`) assign each of them to a variable (`var inputOne = document.getElementByID('id');`) and so on then, times the variables together - `var answer = inputOne * inputTwo` and so on.

Could you please post the html code for the image you showed us so we can show you how to make this work with the code you already have.

Keyboard1333

3. Junior Coders
Join Date
Mar 2012
Posts
8
Thanks
2
Thanked 0 Times in 0 Posts
I cant find any code that works in that manner, that's when I desided to post my problem here.

My knowledge with JavaScript is limited.

The only code that I ever designed was a price calculator and it took me two weeks to figure out. And it was two years ago

This is the script:
[CODE]
<script language="JavaScript"><!--
function Total() {
var q, i, ordertotal, tax, sh, total;
ordertotal = 0;
nitems = 8
for (i=1; i<nitems+1; i++) {
eval("document.orderform.Item" + i + "Total.value = '';");
eval("q = document.orderform.Item" + i + "Quantity.value;");
if (q) {
eval("total=document.orderform.Item" + i + "Price.value * document.orderform.Item" + i + "Quantity.value;");
eval("document.orderform.Item" + i + "Total.value=Currency(total)");
eval("ordertotal = ordertotal + total;");
}
}
document.orderform.OrderTotal.value = Currency(ordertotal);
tax = ordertotal * 0
document.orderform.Tax.value = Currency(tax);
sh = ordertotal * 0
document.orderform.SH.value = Currency(sh=20.00);
document.orderform.GrandTotal.value = Currency(ordertotal + sh);
}

function Currency(anynum) {
//returns number as string in Rxxx,xxx.xx format.
anynum = "" + eval(anynum) //evaluate (in case an expression sent)
intnum = parseInt(anynum) //isolate integer portion
intnum = Math.abs(intnum)
intstr = ""+intnum
//add comma in thousands place.
if (intnum >= 1000) {
intlen = intstr.length
temp1=parseInt(""+(intnum/1000))
temp2=intstr.substring(intlen-3,intlen)
intstr = temp1+","+temp2

}
if (intnum >= 1000000) {
intlen = intstr.length
temp1=parseInt(""+(intnum/1000000))
temp2=intstr.substring(intlen-7,intlen)
intstr = temp1+","+temp2

}

decnum = Math.abs(parseFloat(anynum)-parseInt(anynum)) //isolate decimal portion
decnum = decnum * 100 // multiply decimal portion by 100.
decstr = "" + Math.abs(Math.round(decnum))
if (decstr.length>2) {decstr=decstr.substring(0,2)}
while (decstr.length < 2) {decstr="0"+decstr}
retval = intstr + "." + decstr
if (anynum < 0) {
retval="("+retval+")"
}
return "R"+retval
}
// --></script>
[CODE]
Last edited by sawebs; 03-15-2012 at 05:43 PM.

4. If your trying to times the contents of 4 inputs together then output the answer, this will work.

Code:
```<html>
<head>
<script type="text/javascript">
function calculate() {
var numericExpression = /^[0-9]+\$/;
var field1 = document.getElementById('el1').value;
var field2 = document.getElementById('el2').value;
var field3 = document.getElementById('el3').value;
var field4 = document.getElementById('el4').value;
var check = "true";
if(!field1.match(numericExpression)){
var check = "false";
}
if(!field2.match(numericExpression)){
var check = "false";
}
if(!field3.match(numericExpression)){
var check = "false";
}
if(!field4.match(numericExpression)){
var check = "false";
}
if(check == "false") {
alert("Please enter only numbers");
return;
}
var total = field1 * field2 * field3 * field4;
document.getElementById('calculate').innerHTML = "Total: " + total;
}
</script>
</head>
<body>
<form>
<input type="text" id="el1" style="float:left;" value="Amount of days">
<input type="text" id="el2" style="float:left;" value="Amount of taxis">
<input type="text" id="el3" style="float:left;" value="Price per taxi per day">
<input type="text" id="el4" style="float:left;" value="100% minus % off">
<div id="calculate" style="margin-left:5px;sheight:23px;width:100px;float:left;">Total:</div>
<br />
<br />
<input type="button" value="Calculate" onclick="calculate()">
</form>
</body>
</html>```
Is that what you're trying to do?
Last edited by keyboard; 03-16-2012 at 02:43 AM.

5. Junior Coders
Join Date
Mar 2012
Posts
8
Thanks
2
Thanked 0 Times in 0 Posts
Sorry about the late reply our internet went down over the weekend.

Thank you ferry much for the assistance, you saved me a lot of work and research.

6. You're welcome!

If this thread is finished, please set it to resolved.
You can do this by editing your first post within the thread - Pressing go advanced - then where it says no prefix, selecting resolved then save.
Last edited by keyboard; 03-20-2012 at 02:13 AM.

7. Junior Coders
Join Date
Mar 2012
Posts
8
Thanks
2
Thanked 0 Times in 0 Posts
Hi Keyboard1333

Sorry to be such a pain my client is requesting the following, the code work grate on the website.

In the field '100% minus % off' of the calculator it has to be able to calculate numbers under one, such as 0.93. Than it is calculating the total minus the discount.

8. The only thing that's stopping you from entering a decimal into the 4th field is the validation. All you have to do is tell it to allow dots (.) in the fourth field

Code:
```<html>
<head>
<script type="text/javascript">
function calculate() {
var numericExpression = /^[0-9]+\$/;
`var numericExpression2 = /^[0-9\.]+\$/;`
var field1 = document.getElementById('el1').value;
var field2 = document.getElementById('el2').value;
var field3 = document.getElementById('el3').value;
var field4 = document.getElementById('el4').value;
var check = "true";
if(!field1.match(numericExpression)){
var check = "false";
}
if(!field2.match(numericExpression)){
var check = "false";
}
if(!field3.match(numericExpression)){
var check = "false";
}
if(!field4.match(`numericExpression2`)){
var check = "false";
}
if(check == "false") {
alert("Please enter only numbers");
return;
}
var total = field1 * field2 * field3 * field4;
document.getElementById('calculate').innerHTML = "Total: " + total;
}
</script>
</head>
<body>
<form>
<input type="text" id="el1" style="float:left;" value="Amount of days">
<input type="text" id="el2" style="float:left;" value="Amount of taxis">
<input type="text" id="el3" style="float:left;" value="Price per taxi per day">
<input type="text" id="el4" style="float:left;" value="100% minus % off">
<div id="calculate" style="margin-left:5px;sheight:23px;width:100px;float:left;">Total:</div>
<br />
<br />
<input type="button" value="Calculate" onclick="calculate()">
</form>
</body>
</html>```

9. Junior Coders
Join Date
Mar 2012
Posts
8
Thanks
2
Thanked 0 Times in 0 Posts
Sorry about the late reply they have been putting optical fiber cabling into our building.

The form needs to do the following?

Subtract that from 100 and type the result in the percentage box.
MUST BE: Subtract that from 100 % (example outcome: 0.93) and type
that result in the percentage box.

Current result is: 22 x 50 x 15 -10% Total: 165000
I need it to calculate in this manner: 22 x 50 x 15 -10% Total: 14850

Thanks for all your help.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•