PDA

View Full Version : Need help with a auto calculating form



sawebs
04-16-2012, 07:12 AM
I have posted this topic before the code works 100% but not the way my client wants it.

The code was created by (keyboard1333) on this form.

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

this is the 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>


Any Help will be appreciated. Thanks

jscheuer1
04-16-2012, 09:50 AM
<html>
<head>
<script type="text/javascript">
function calculate() {
var fields = [], i = 1, el, val, whole, total;
for (i; i < 5; ++i){
el = document.getElementById('el' + i); val = el.value;
if((whole = i < 3) && /\./.test(val) || isNaN(Number(val))){
alert("Please enter only " + (whole? 'whole ' : '') + "numbers\nfor " + (el.value = el.defaultValue));
el.focus();
el.select();
return;
}
fields.push(val);
}
total = fields[0] * fields[1] * fields[2];
total -= fields[3]/100 * total;
document.getElementById('calculate').innerHTML = "Total: " + total;
}
</script>
<style type="text/css">
label {
float: left;
margin-right: 3px;
}
#calaculate {
float: left;
height: 23px;
width: 100px;
}
</style>
</head>
<body>
<form action="#" onsubmit="calculate(); return false;">
<label>Days: <input type="text" id="el1" value="Amount of days"></label>
<label>Taxis: <input type="text" id="el2" value="Amount of taxis"></label>
<label>Perdiem: <input type="text" id="el3" value="Price per taxi per day"></label>
<label>Discount: <input type="text" id="el4" value="100% minus % off"></label>
<div id="calculate">Total:</div>
<br />
<br />
<input type="submit" value="Calculate">
</form>
</body>
</html>