Advanced Search

Results 1 to 1 of 1

Thread: Auto calculating form?

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

    Question Auto 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.
    Last edited by djr33; 04-26-2012 at 06:58 AM. Reason: fixed [/code] tags

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
  •