www.webdeveloper.com
Results 1 to 8 of 8

Thread: text field correlation and calculations

  1. #1
    Join Date
    Feb 2009
    Posts
    7

    text field correlation and calculations

    hi everyone, a friend asked me to overlook a javascript problem and I don't know where to begin. He has a page where you enter a number in text fields for quantity and then a javascript function will take those numbers, place them in text fields at the bottom and then use those numbers to calculate the price. In turn take that price and place it in the total field.

    right now some numbers come up as NaN, some work, some don't show up at all. I was trying to find new functions completely to put in there to complete the tasks if I couldn't fix it and I don't even know where to begin.

    http://www.documentlabels.com is the website.

    If you can point me in the direction of a place to find functions I can copy and paste in place or maybe help me find the problem I really appreciate it.

  2. #2
    Join Date
    Oct 2007
    Posts
    175
    First pass the value from initial field

    HTML Code:
    <html>
    <body>
    Qty<br>
    
    Apples <input type="text" id="t1" name="t1" value="0" onkeyup="q1.value=this.value"><br>
    kiwis <input type="text" id="t2" name="t2" value="0" onkeyup="q2.value=this.value"><br>
    Pears <input type="text" id="t3" name="t3" value="0" onkeyup="q3.value=this.value"><br>
    <br><br>
    <input type="text" id="q1" name="q1" value="0"><br>
    <input type="text" id="q2" name="q2" value="0"><br>
    <input type="text" id="q3" name="q3" value="0">
    
    
    </body>
    </html>
    Then post your current calculation script and we can work from there.

  3. #3
    Join Date
    Feb 2009
    Posts
    7
    heres the functions, maybe your eyes will catch something mine are skipping over.

    Code:
    <script language=javascript>
    <!--
      	function CheckKey(e) {
      	  	if (!((e.keyCode > 47) && (e.keyCode < 58)) && !((e.keyCode > 95) && (e.keyCode < 106)) && (e.keyCode != 8) && (e.keyCode != 39) && (e.keyCode != 37) && (e.keyCode != 13) && (e.keyCode != 9)) {
      			e.returnValue = false;
      			return;
      		};
      	}
      	function BlockAll(e) {
      		e.returnValue = false;
      		return;
      	}
      	function CalculateDocumentLabels() {
    		var first = 1;
    		var last = 38;
    		var total = 0.0;
    		
    		for (i=first;i<=last;i++) {
    			// Go through all the elements for this section and add the quantities
    			e = document.FrontPage_Form1.elements(i).value;
    			if (e == "") {
    				e = 0.0;
    			};
    			total += Number(e);
    		};
      		document.FrontPage_Form1.total_quantity_document_labels.value = Number(total);
      		CalculateTotals();
      	}
    	
      	function CalculatePageTabs() {
    		var first = 39;
    		var last = 41;
    		var total = 0.0;
    		
    		for (i=first;i<=last;i++) { 
    			// Go through all the elements for this section and add the quantities
    			e = document.FrontPage_Form1.elements(i).value;
    			if (e == "") {
    				e = 0.0;
    			};
    			total += Number(e);
    		};
      		document.FrontPage_Form1.total_quantity_page_tabs.value = Number(total);
      		CalculateTotals();
      	}
    	
    	function CalculateReceiptables() {
    		var first = 43;
    		var last = 43;
    		var total = 0.0;
    		
    		for (i=first;i<=last;i++) { 
    			// Go through all the elements for this section and add the quantities
    			e = document.FrontPage_Form1.elements(i).value;
    			if (e == "") {
    				e = 0.0;
    			};
    			total += Number(e);
    		};
      		document.FrontPage_Form1.total_quantity_receiptabels.value = Number(total);
      		CalculateTotals();
      	}
    	
    	function CalculateOPE() {
    		var first = 44;
    		var last = 44;
    		var total = 0.0;
    		
    		for (i=first;i<=last;i++) { 
    			// Go through all the elements for this section and add the quantities
    			e = document.FrontPage_Form1.elements(i).value;
    			if (e == "") {
    				e = 0.0;
    			};
    			total += Number(e);
    		};
      		document.FrontPage_Form1.total_quantity_out_of_pocket_expenses.value = Number(total);
      		CalculateTotals();
      	}
    	
    	function CalculateBMEE() {
    		var first = 45;
    		var last = 45;
    		var total = 0.0;
    		
    		for (i=first;i<=last;i++) { 
    			// Go through all the elements for this section and add the quantities
    			e = document.FrontPage_Form1.elements(i).value;
    			if (e == "") {
    				e = 0.0;
    			};
    			total += Number(e);
    		};
      		document.FrontPage_Form1.total_quantity_business_meals_entertainment_expenses.value = Number(total);
      		CalculateTotals();
      	}
    	
    	function CalculateMeditraxx() {
    		var first = 48;
    		var last = 48;
    		var total = 0.0;
    		
    		for (i=first;i<=last;i++) {
    			// Go through all the elements for this section and add the quantities
    			e = document.FrontPage_Form1.elements(i).value;
    			if (e == "") {
    				e = 0.0;
    			};		
    			total += Number(e);
    		};	
      		document.FrontPage_Form1.total_quantity_meditraxx.value = Number(total);
      		CalculateTotals();
      	}
    	
      	function FormatCurrency(val) {
      		// Force 2 decimals
    		var strval = String(val);
    		var pos = strval.lastIndexOf(".");
    		
    		if (pos == -1) {
    			// the decimal point is not there, add it
    			strval += ".00";
    		}
    		else if (strval.length-pos > 2) {
    			strval = strval.substring(0, pos+3);
    		};
    		else if (pos < (strval.length-1)) {
    			var totalloops = strval.length-pos-1;
    			for (i=0;i<totalloops;i++) {
    				strval += "0";
    			};
    		};
    		return strval;
      	}
      	
      	function CalculateTotals() {
      		// Calculate Document Labels
      		var doctotal = document.FrontPage_Form1.total_quantity_document_labels.value * 4.79;
      		document.FrontPage_Form1.total_document_labels.value = "$" + FormatCurrency(doctotal);
      		
      		// Calculate Page Tabs
      		var pagetotal = document.FrontPage_Form1.total_quantity_page_tabs.value * 4.79;
      		document.FrontPage_Form1.total_page_tabs.value = "$" + FormatCurrency(pagetotal);
      		
    		// Calculate Receiptabels
    		var receiptotal = document.FrontPage_Form1.total_quantity_receiptabels.value * 4.95;
      		document.FrontPage_Form1.total_receiptabels.value = "$" + FormatCurrency(receiptotal);
    		
    		// Calculate Out of Pocket Expenses
    		var opetotal = document.FrontPage_Form1.total_quantity_out_of_pocket_expenses.value * 2.95;
      		document.FrontPage_Form1.total_out_of_pocket_expenses.value = "$" + FormatCurrency(opetotal);
    		
    		// Calculate Business Meals and Entertainment Expenses
    		var bmeetotal = document.FrontPage_Form1.total_quantity_business_meals_entertainment_expenses.value * 2.95;
      		document.FrontPage_Form1.total_business_meals_entertainment_expenses.value = "$" + FormatCurrency(bmeetotal);
    		
    		// Calculate Meditraxx
      		var meditotal = document.FrontPage_Form1.total_quantity_meditraxx.value * 0.99;
      		document.FrontPage_Form1.total_meditraxx.value = "$" + FormatCurrency(meditotal);
      		
      		var subtotal = doctotal + pagetotal + receiptotal + opetotal + bmeetotal + meditotal;
      		
      		// Calculate Shipping
      		var shipping = 0.0;
      		
      		if ((subtotal > 0) && (subtotal <= 12.00)) {
      			shipping = 1.49;
      		}
      		else if ((subtotal > 12.00) && (subtotal < 25.00)) {
      			shipping = 1.49;
      		};
      		
      		document.FrontPage_Form1.total_shipping.value = "$" + FormatCurrency(shipping);	
      		document.FrontPage_Form1.total_price.value = "$" + FormatCurrency((subtotal + shipping));
      	}
    // -->
    </script>

  4. #4
    Join Date
    Oct 2007
    Posts
    175
    Here is a link I had questions with a while ago which was solved, it may help, it may not:

    http://www.webdeveloper.com/forum/sh...d.php?t=187537

    What I can see if you #'s are coming out to NaN then it is not reading it as a #, look up parseInt() which sets a # to read as a whole number.

  5. #5
    Join Date
    Feb 2009
    Posts
    7
    Thanks for the help but I'm still clueless on this, I'm about to stop trying to fix it all together and just look for new functions I can copy and paste in there.

  6. #6
    Join Date
    Feb 2009
    Location
    Tempe, AZ
    Posts
    4
    I think the NaN problem is the e == ""... if e is not a number you want to set it to 0, not just if it's equal to ""... What I like to do to solve problems like this is load up Firebug, go to the console tab, and manualy test parts of the code, figure out what inputs create the NaN, then find where in the code the NaN comes from (NaN means "Not A Number").. Again, my guess it is the e == ""... You need to make sure it's a number.. If it isn't a number, say something, or make it zero...

    So... instead of if(e == "") change it to if(isNaN(e)){ e = 0.0; } and I think that will do it.

    Debugging is always tough. But this code is pretty simple, so you should be able to figure it out. And the above might solve the problem.

  7. #7
    Join Date
    Feb 2009
    Posts
    7
    Thank you for the help and the patience, if this doesn't do anything I'll be back for sure. Thanks again.

  8. #8
    Join Date
    Nov 2006
    Posts
    528
    No, offense, but that code is seriously overblown.

    There are many different functions for totally the quantity for a number of fields and populating that total into another field. Instead of having all those functions that do the same thing, just have one function where you pass the start field, the end field, and the field to populate the total.
    Code:
    	function calcQty(first, last, totalFieldID)
    	{
    		var total = 0;
    		
    		for (var i=first; i<=last; i++) {
    			// Go through all the elements for this section and add the quantities
    			e = document.FrontPage_Form1.elements[i];
    			if(parseInt(e.value))
    			{
    				total += parseInt(e.value);
    			}
    		};
      		document.FrontPage_Form1[totalFieldID].value = total;
      		CalculateTotals();
    	}
    Then you would replace this
    Code:
    onKeyUp="CalculateDocumentLabels();"
    With this
    Code:
    onKeyUp="calcQty(1, 38, 'total_quantity_document_labels');"
    The same would be done for all the other "quantity" calculation function calls.

    Also, the format currency function is way more complex than it should be when all you need is this
    Code:
      	function FormatCurrency(val) {
    		return val.toFixed(2);
      	}
    Also, some of the start/end field values are not correct. The one I found was the "tabs" calculations starts/ends with 39/41 when it shoudl be 41/43. Using the index numbers of the fields is not a good way to do this anyway. I have seen where some browsers will count some elements in a different manner. I would suggest giving all the field names or IDs a prefix and you could use the prefix to determine which ones to add.
    Last edited by mjdamato; 02-05-2009 at 05:00 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles