CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Deploying Windows Server 2008 with System Center
  • Remote Desktop Protocol Performance Improvements in Windows Server 2008 R2 and Windows 7
  • The Microsoft Dynamics CRM Security Model
  • SQL Server Modeling Services with Microsoft Visual Studio 2010 Beta 2

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Other Programming > Scripting - Client Side
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    Scripting - Client Side Discuss client-side scripting issues. Client-side scripting such as JavaScript, JScript, and VBScript as well as technologies such as HTML and stylesheets.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old November 20th, 2009, 11:28 AM
    versacestl versacestl is offline
    Member
     
    Join Date: Mar 2007
    Posts: 77
    versacestl is an unknown quantity at this point (<10)
    Javascript not firing

    What am I doing wrong here?
    First function is working, but i am getting an error saying the second is not a function..

    Code:
    <html>
    	<head>
    		<title>Mortgage Calculator</title>
    	</head>
    	<body>
    	<script type="text/javascript" language="JavaScript">
    		function onlyNumbers(evt)
    		{
    			var e = event || evt;
    			var charCode = e.which || e.keyCode;
    
    			if (charCode > 31 && (charCode < 46 || charCode > 57))
    				return false;
    
    			return true;
    
    		}
    
    		function mortgageCalc(form)
    		{
    			var LoanAmount= form.LoanAmount.value;
    			var InterestRate = form.LoanAmount.value / 100 / 12;
    			var NumberOfPayments = form.NumberOfYears.value * 12;
    
    			var x = Math.pow(1 + InterestRate, payments);
    			var monthlyPayment = ((LoanAmount * x * InterestRate) / (x - 1));
    
    			if (!isNaN(NumberOfPayments) && (monthlyPayment != Number.POSITIVE_INFINITY) && (monthlyPayment != Number.NEGATIVE_INFINITY))
    			{
    				document.form.calcNumberOfPayments.value = monthlyPayment;
    				document.form.calcMonthlyPayment.value = monthlyPayment * NumberOfPayments;
    			}
    			else
    			{
    				document.form.calcNumberOfPayments.value = "";
    				document.form.calcMonthlyPayment.value = "";
    			}
    		}
    
    	</script>
    
    		<form name="myForm">
    
    			<table border="1" align="center">
    				<tr>
    					<td align="center" colspan="2">
    						<font color="black" size="2" face="Verdana">
    						<b>Mortgage Calculator</b>
    					</td>
    				</tr>
    				<tr>
    					<td>
    						<table border="0" cellpadding="2">
    							<tr>
    								<td align="left">
    									<font color="black" size="2" face="Verdana">Amount to Borrow:</font>
    								</td>
    								<td>
    									<font color="black" size="2" face="Verdana">
    										<input type="text" size="7" maxlength="8" name="LoanAmount" value="25000" onkeypress="return onlyNumbers();"><br>
    									</font>
    								</td>
    							</tr>
    							<tr>
    								<td align="left">
    									<font color="black" size="2" face="Verdana">Annual Percentage Rate:</font>
    								</td>
    								<td>
    									<font color="black" size="2" face="Verdana">
    										<input type="text" size="2" maxlength="4" name="InterestRate" value="5.4" onkeypress="return onlyNumbers();"><br>
    									</font>
    								</td>
    							</tr>
    							<tr>
    								<td align="left">
    									<font color="black" size="2" face="Verdana">Length of Loan (Years):</font>
    								</td>
    								<td>
    									<font color="black" size="2" face="Verdana">
    										<input type="text" maxlength="3" size="2" name="NumberOfYears" value="4" onkeypress="return onlyNumbers();">
    									</font>
    								</td>
    							</tr>
    							<tr>
    								<td colspan="2" align="center">
    									<font color="black" size="2" face="Verdana"><br>
    										<input type="button" name="mortgageCalc" value="Calculate" onclick="mortgageCalc(myForm);">
    									</font>
    								<td>
    							</tr>
    							<tr>
    								<td colspan="3">
    									<font color="black"></font>
    								</td>
    							</tr>
    						</table>
    					</td>
    				</tr>
    				<tr>
    					<td><br>
    						<table border="0" cellpadding="2">
    							<tr>
    								<td align="right">
    									<font color="black" size="2" face="Verdana">Number of Payments:</font>
    								</td>
    								<td>
    									<font color="black" size="2" face="Verdana">
    										<input type="text" size="7" name="calcNumberOfPayments" style="border:0;" Disabled><br>
    									</font>
    								</td>
    							</tr>
    							<tr>
    								<td align="right">
    									<font color="black" size="2" face="Verdana">Monthly Payment:</font>
    								</td>
    								<td>
    									<font color="black" size="2" face="Verdana">
    										<input type="text" size="7" name="calcMonthlyPayment" style="border:0;" Disabled><br>
    									</font>
    								</td>
    							</tr>
    							<tr>
    								<td align="center" colspan="2">&nbsp;</td>
    							</tr>
    						</table>
    					</td>
    				</tr>
    			</table>
    		</form>
    	</body>
    </head>
    Reply With Quote
      #2    
    Old November 20th, 2009, 11:40 AM
    PeejAvery's Avatar
    PeejAvery PeejAvery is offline
    Super Moderator
    Power Poster
     
    Join Date: May 2002
    Location: United States
    Posts: 9,168
    PeejAvery has a brilliant future (2000+) PeejAvery has a brilliant future (2000+) PeejAvery has a brilliant future (2000+) PeejAvery has a brilliant future (2000+) PeejAvery has a brilliant future (2000+) PeejAvery has a brilliant future (2000+) PeejAvery has a brilliant future (2000+) PeejAvery has a brilliant future (2000+) PeejAvery has a brilliant future (2000+) PeejAvery has a brilliant future (2000+) PeejAvery has a brilliant future (2000+)
    Re: Javascript not firing

    Code:
    function onlyNumbers(evt) {
      var charCode = (evt.which) ? evt.which : event.keyCode
      if (charCode > 31 && (charCode < 48 || charCode > 57)) {return false;}
      else {return true;}
    }
    __________________
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.
    Reply With Quote
      #3    
    Old November 20th, 2009, 12:35 PM
    versacestl versacestl is offline
    Member
     
    Join Date: Mar 2007
    Posts: 77
    versacestl is an unknown quantity at this point (<10)
    Re: Javascript not firing

    I need decimals so i did: > 46.
    Also, has nothing to do with the mortgageCalc() not working
    Reply With Quote
      #4    
    Old November 20th, 2009, 01:55 PM
    PeejAvery's Avatar
    PeejAvery PeejAvery is offline
    Super Moderator
    Power Poster
     
    Join Date: May 2002
    Location: United States
    Posts: 9,168
    PeejAvery has a brilliant future (2000+) PeejAvery has a brilliant future (2000+) PeejAvery has a brilliant future (2000+) PeejAvery has a brilliant future (2000+) PeejAvery has a brilliant future (2000+) PeejAvery has a brilliant future (2000+) PeejAvery has a brilliant future (2000+) PeejAvery has a brilliant future (2000+) PeejAvery has a brilliant future (2000+) PeejAvery has a brilliant future (2000+) PeejAvery has a brilliant future (2000+)
    Re: Javascript not firing

    When I first copied your code, the first function wasn't working for me, so I didn't even go any further.

    1. I honestly don't know why, so just change the name to calcMortgage. That fixes that part.

    2. In the funtion onlyNumbers()...You need to be passing the actual event for the first function to fire properly in all browsers.

    Code:
    <input type="text" size="7" maxlength="8" name="LoanAmount" value="25000" onkeypress="return onlyNumbers(event);">
    3. In the function mortgageCalc()...You never specified payments before the following line. So that is throwing an undefined error as well.

    Code:
    var x = Math.pow(1 + InterestRate, payments);
    4. In the function mortgageCalc()...You are already passing the actual form through the parameter, so document.form... is invalid in the following parts. Make is just form...

    Code:
    			if (!isNaN(NumberOfPayments) && (monthlyPayment != Number.POSITIVE_INFINITY) && (monthlyPayment != Number.NEGATIVE_INFINITY))
    			{
    				document.form.calcNumberOfPayments.value = monthlyPayment;
    				document.form.calcMonthlyPayment.value = monthlyPayment * NumberOfPayments;
    			}
    			else
    			{
    				document.form.calcNumberOfPayments.value = "";
    				document.form.calcMonthlyPayment.value = "";
    			}
    __________________
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.
    Reply With Quote
      #5    
    Old November 20th, 2009, 02:45 PM
    versacestl versacestl is offline
    Member
     
    Join Date: Mar 2007
    Posts: 77
    versacestl is an unknown quantity at this point (<10)
    Re: Javascript not firing

    Ok. It seems to be calculating correctly now.. Here is the current code

    Code:
    <html>
    	<head>
    		<title>Mortgage Calculator</title>
    	</head>
    	<body>
    	<script type="text/javascript" language="JavaScript">
    		function onlyNumbers(evt)
    		{
    			var e = event || evt;
    			var charCode = e.which || e.keyCode;
    
    			if (charCode > 31 && (charCode < 46 || charCode > 57))
    				return false;
    
    			return true;
    		}
    
    		function calcPayment(Loan, Interest, Length)
    		{
    			var result = (Math.floor((Loan * Interest) / (1 - Math.pow(1 + Interest, (-1 * Length))) * 100) / 100);
    			return result;
    		}
    
    
    		function roundNumber(value, decimals)
    		{
    			var result = Math.round(value * Math.pow(10, decimals)) / Math.pow(10, decimals);
    			return result;
    		}
    
    		function calcMortgage(form)
    		{
    			var LoanAmount= form.LoanAmount.value;
    			var InterestRate = ((form.InterestRate.value / 100) / 12);
    			var NumberOfPayments = (form.NumberOfYears.value * 12);
    
    			var monthlyPayment = calcPayment(LoanAmount, InterestRate / 12, NumberOfPayments);
    
    			if (!isNaN(NumberOfPayments) && (monthlyPayment != Number.POSITIVE_INFINITY) && (monthlyPayment != Number.NEGATIVE_INFINITY))
    			{
    				form.calcNumberOfPayments.value = NumberOfPayments;
    				form.calcMonthlyPayment.value = roundNumber(monthlyPayment, 2);
    			}
    			else
    			{
    				form.calcNumberOfPayments.value = "";
    				form.calcMonthlyPayment.value = "";
    			}
    		}
    
    	</script>
    
    		<form name="myForm">
    
    			<table border="1" align="center">
    				<tr>
    					<td align="center" colspan="2">
    						<font color="black" size="2" face="Verdana">
    						<b>Mortgage Calculator</b>
    					</td>
    				</tr>
    				<tr>
    					<td>
    						<table border="0" cellpadding="2">
    							<tr>
    								<td align="left">
    									<font color="black" size="2" face="Verdana">Amount to Borrow:</font>
    								</td>
    								<td>
    									<font color="black" size="2" face="Verdana">
    										<input type="text" size="7" maxlength="8" name="LoanAmount" value="" onkeypress="return onlyNumbers();"><br>
    									</font>
    								</td>
    							</tr>
    							<tr>
    								<td align="left">
    									<font color="black" size="2" face="Verdana">Annual Percentage Rate:</font>
    								</td>
    								<td>
    									<font color="black" size="2" face="Verdana">
    										<input type="text" size="2" maxlength="4" name="InterestRate" value="" onkeypress="return onlyNumbers();"><br>
    									</font>
    								</td>
    							</tr>
    							<tr>
    								<td align="left">
    									<font color="black" size="2" face="Verdana">Length of Loan (Years):</font>
    								</td>
    								<td>
    									<font color="black" size="2" face="Verdana">
    										<input type="text" maxlength="3" size="2" name="NumberOfYears" value="" onkeypress="return onlyNumbers(); ">
    									</font>
    								</td>
    							</tr>
    							<tr>
    								<td colspan="2" align="center">
    									<font color="black" size="2" face="Verdana"><br>
    										<input type="button" name="mortgageCalc" value="Calculate" onClick="calcMortgage(myForm);">
    									</font>
    								<td>
    							</tr>
    							<tr>
    								<td colspan="3">
    									<font color="black"></font>
    								</td>
    							</tr>
    						</table>
    					</td>
    				</tr>
    				<tr>
    					<td><br>
    						<table border="0" cellpadding="2">
    							<tr>
    								<td align="right">
    									<font color="black" size="2" face="Verdana">Number of Payments:</font>
    								</td>
    								<td>
    									<font color="black" size="2" face="Verdana">
    										<input type="text" size="7" name="calcNumberOfPayments" style="border:0;" Disabled><br>
    									</font>
    								</td>
    							</tr>
    							<tr>
    								<td align="right">
    									<font color="black" size="2" face="Verdana">Monthly Payment:</font>
    								</td>
    								<td>
    									<font color="black" size="2" face="Verdana">
    										<input type="text" size="7" name="calcMonthlyPayment" style="border:0;" Disabled><br>
    									</font>
    								</td>
    							</tr>
    							<tr>
    								<td align="center" colspan="2">&nbsp;</td>
    							</tr>
    						</table>
    					</td>
    				</tr>
    			</table>
    		</form>
    	</body>
    </head>

    Last edited by versacestl; November 20th, 2009 at 03:18 PM.
    Reply With Quote
      #6    
    Old November 20th, 2009, 03:02 PM
    versacestl versacestl is offline
    Member
     
    Join Date: Mar 2007
    Posts: 77
    versacestl is an unknown quantity at this point (<10)
    Re: Javascript not firing

    Anyone have some more error checking i can do?

    Last edited by versacestl; November 20th, 2009 at 03:40 PM.
    Reply With Quote
      #7    
    Old November 20th, 2009, 03:18 PM
    versacestl versacestl is offline
    Member
     
    Join Date: Mar 2007
    Posts: 77
    versacestl is an unknown quantity at this point (<10)
    Re: Javascript not firing

    Resolved

    Last edited by versacestl; November 20th, 2009 at 03:41 PM.
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Other Programming > Scripting - Client Side


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 03:02 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.