#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    31
    Rep Power
    2

    JavaScript forms


    Two part question here, I am making a loan payment calculator and have a few problems.

    First and most importantly, the "amount" and "months" fields do not seem to be recognized by the calculation function 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.

    Also, I have each field calling a different function to check to be sure the contents are valid, so the error gets placed next to the right field, if there's a way to call one function and have the error placed in the right place that would be good, but the main thing is just to get the calculations working! (PS, I know that the "else" part of the validation functions is only on the first field, haven't gotten to putting it on the other two yet)

    Here are links to the files:
    http://tiger.towson.edu/~tbaldw2/LoanCalc.html
    http://tiger.towson.edu/~tbaldw2/LoanCalc.js
    Last edited by me10lee83; May 15th, 2013 at 12:02 PM.
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,142
    Rep Power
    120
    Here is the example; that your code is based on: http://www.java2s.com/Code/JavaScrip...Calculator.htm. And... just as a side note; if I was turning this in for "homework"; I would try to create my own code and not turn in "example code". I think that falls under a "honor code volition" and can get you expelled.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    Sydney Australia
    Posts
    183
    Rep Power
    83
    Originally Posted by me10lee83

    First and most importantly, the "amount" and "months" fields do not seem to be recognized by the calculation function and I'm not sure why.
    You are using getElementById(), but your elements only names, not id

    should be
    Code:
    <input type="text" onblur="validate1(this)" name="amount" id="amount">
    Then getElementById("amount") will find the element for you.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    31
    Rep Power
    2
    Originally Posted by web_loone08
    Here is the example; that your code is based on: http://www.java2s.com/Code/JavaScrip...Calculator.htm. And... just as a side note; if I was turning this in for "homework"; I would try to create my own code and not turn in "example code". I think that falls under a "honor code volition" and can get you expelled.
    Our professor has said we are allowed to use libraries, examples, etc as long as we list the source and don't try to claim it as our own, then that's plageurism, but as long as it's cited it's ok

    Originally Posted by BarryG
    You are using getElementById(), but your elements only names, not id
    Ah ha, knew it was something simple. Thanks :-)
    Last edited by me10lee83; May 18th, 2013 at 08:09 AM.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    31
    Rep Power
    2
    Actually that was not the issue. The form fields did not have ids, but the getElementById was not referring to the form field, it was inserting an error message when necessary in a different table cell (which did have the id, and that function was working).

    My issue was with a different function, the OnSubmit one. The problem was in the if statement, the wrong variable was being assigned to the payment name field! Duh, knew it was something simple. Everything works now
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    31
    Rep Power
    2
    I got form working, and I got the validation working (with my own code this time, changed the examples so much it didn't even resemble the original anymore) but would like to expand what it checks for and am having some trouble doing this.

    It currently checks the values are within a set range and display particular error messages if not (depending on its too high, too low, or empty). I would also like it to check that the value is actually a number. I tried using isNaN but it's not working.

    Also, I would like the calculation to occur ONLY if all the fields pass. I tried using an if statement to check if the function is true (and the validate function returns true or false based on the result) but it doesn't work.

    Here are a link to the files
    http://tiger.towson.edu/~tbaldw2/loan_calculator/LoanCalc.html
    http://tiger.towson.edu/~tbaldw2/loan_calculator/LoanCalc.js
    (working files)

    http://tiger.towson.edu/~tbaldw2/loan_calculator/LoanCalc1.html
    http://tiger.towson.edu/~tbaldw2/loan_calculator/LoanCalc1.js (with the non-working changes)
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    31
    Rep Power
    2
    Still working on this, I thought I came up with a way to handle the "only return calculation if no errors", though it's not working.

    Below is the OnSubmit function code. For the HTMl/full JavaScript see the links above (the LoanCalc.js is the original that works, LoanCal2.js at the same location has these latest changes)

    Code:
    function OnSubmit(form) 
    {
        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;
    	
    	payment = Math.round(payment*100)/100 //Rounds the "payment" number to 2 decimal places
    	message = "Your payment will be: $"+payment+" a month." //Sets the output to the message plus the payment value  
    	
    	if (amount < 100 || amount > 1000000 || amount="" || amount=" ")
    	{
    	validate1();
    	return false;
    	}
    	
    	else if (apr < .1 || apr < 10 || apr="" || apr=" ") 
    	{
    	validate2();
    	return false;
    	}
    	
    	else if (months < 1 || months > 600 || months="" || months=" ")
    	{	
    	validate3();
    	return false
    	}
    	
    	else
    	{
    	document.getElementById("results").innerHTML=message;
    	return false;
    	)
    }
    I'm more focused on getting the validation working than checking that a number was entered at the moment so if anyone has insight as to why this code isn't working please help me.
    Last edited by me10lee83; May 16th, 2013 at 02:02 PM.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    1
    Rep Power
    0
    Originally Posted by me10lee83
    Code:
    var pvifa=(1.0/ monthly_int)-(1.0/( monthly_int *Math.pow(1.0+ monthly_int, months)));
    I am not sure about the error you are getting but your financial calculations do not seem right specifically the PVIFA formula looks incorrect

    A correct version is as follows

    Code:
    var pvifa= (1.0 - Math.pow(1.0+ monthly_int, -months)) / monthly_int;

IMN logo majestic logo spyfu logo threadwatch logo seochat tools logo