Results 1 to 2 of 2

Thread: Calculating form

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

    Unhappy Calculating form

    The form is working 100% fine but not the correct answer.

    I have no idea what i can change in the script to get the right answer.

    10 days x 10 taxis x 15 (price for ad) x 0.93 (100% minus discount) = 1,395

    The calculator's current answer is = 1,486.05

    The script was created bye jscheuer1.

    The javascript

    Code:
    <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>
    And the Body

    Code:
    <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>
    My client is driving me up the will for this calculating form.

    Please any help is appreciated.

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,794
    Thanks
    2
    Thanked 419 Times in 413 Posts

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <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])*fields[3]/100;
    	document.getElementById('calculate').innerHTML = "Total: " + total;
    }
    </script></head>
    
    <body>
    <form action="#" onSubmit="calculate(); return false;">
    <label>Days: <input type="text" id="el1" value="10"></label>
    <label>Taxis: <input type="text" id="el2"  value="10"></label>
    <label>Perdiem: <input type="text" id="el3" value="15"></label>
    <label>Discount: <input type="text" id="el4" value="93"></label>
    <div id="calculate">Total:</div>
    <br />
    <br />
    <input type="submit" value="Calculate">
    </form>
    </body>
    
    </html>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

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

    sawebs (05-04-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
  •