Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    May 2013
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Function only reading 1 of 3 fields

    I am making a loan payment calculator and have a problem (then maybe a follow up "how to simplify" question, but I'll start with the problem).

    The "amount" and "months" fields do not seem to be recognized by the OnSubmit function, only the "apr" field and I'm not sure why. It's probably just a simple typo but I've looked over it a few times and don't see the issue.

    (PS, I apologize if this message has been posted multiple times, this is my 3rd try and it keeps not showing up after I submit it!)

  • #2
    New Coder
    Join Date
    May 2013
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ok it finally posted! Let me try to add the links to the files now...

    Here are links to the files:
    http://tiger.towson.edu/~tbaldw2/LoanCalc.html
    http://tiger.towson.edu/~tbaldw2/LoanCalc.js

  • #3
    New Coder
    Join Date
    May 2013
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ah I see the problem, I was trying to link to the html and js files in question, but looks like an administrator might need to approve those first? The files are a little long to post the full script here, so I posted the relevant snippets in the meantime and maybe I can share links via PM if needed?

    Code:
    <form name="loandata">
      <table>
        <tr>
    	  <th colspan="4">Enter Loan Information:</b></th>
    	</tr>
        <tr>
          <td class="label"><label for="amount">Loan Amount:</label></td>
    	  <td>$</td>
          <td><input type="text" name="amount" onBlur="validate1(this)"></td>
    	  <td class="error" id="amount"></td>
        </tr>
        <tr>
          <td class="label"><label for="apr">Annual Percentage Rate (APR):</label></td>
    	  <td></td>
          <td><input type="text" name="apr" onBlur="validate2(this)">% </td>
    	  <td class="error" id="apr"></td>
        </tr>
        <tr>
          <td class="label"><label for="months">Months to pay:</label></td>
    	  <td></td>
          <td><input type="text" name="months" onBlur="validate3(this)"></td>
    	  <td class="error" id="months"></td>
        </tr>
        <tr>
    	  <td class="button"><input type="button" value="Calc Payment" onClick="OnSubmit();"></td>
    	  <td></td>
    	  <td class="button"><input type="reset" value="Reset"></td>
    	</tr>
        <tr>
    	  <th colspan="3">Payment Information:</th>
    	</tr>
        <tr>
          <td>Your monthly payment will be:</td>
    	  <td></td>
          <td><input type="text" name="payment"></td>
        </tr>
      </table>
    </form>
    Code:
    function OnSubmit() {
        var amount = document.loandata.amount.value;   //Sets the value from the "amount of loan" field to the variable amount
        var apr = document.loandata.apr.value; 		   //Sets the value of the apr field to the apr variable
        var months = document.loandata.months.value;   //Sets the months to pay field value to the months variable
    
    	var monthly_int = apr/1200.0; /* interest divided by 12 months * 100 percent */
    	var pvifa=(1.0/ monthly_int)-(1.0/( monthly_int *Math.pow(1.0+ monthly_int, months)));
    	payment = amount/pvifa;
    	
        // Check that the result is a finite number. If so, display the results
        if (!isNaN(monthly_int) &&  (monthly_int != Number.POSITIVE_INFINITY) && (monthly_int != Number.NEGATIVE_INFINITY)) {
            document.loandata.payment.value = monthly_int;
        }
        // Otherwise, the user's input was probably invalid, so display an error
        else {
            document.loandata.payment.value = "Invalid";
    		alert("Invalid input, please input numbers only")
        }
    }


  •  

    Posting Permissions

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