Results 1 to 2 of 2

Thread: Need help with a auto calculating form

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

    Question Need help with a auto calculating form

    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:

    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
    Last edited by jscheuer1; 04-16-2012 at 09:28 AM. Reason: Format

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    <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>
    Last edited by jscheuer1; 04-17-2012 at 10:51 AM.

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    sawebs (04-20-2012)

Bookmarks

Posting Permissions

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