How to Calculate Amortization Schedule for your loan account(Using javascript)

Step-1:
We need a html Form to enter The value we want to calculate.

Html code for the form to input loan amount

Open your favorite code editor paste this code,save it as

form.html

 </code><html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>
 Loan account Schedule
</title>
<script language="javascript" src="loan_schedule.js" type="text/javascript"> </script>
</head>
<body id="inside">
<form name="aspnetForm" method="post"  id="aspnetForm">
 <div>
 <div>
 <div>
 <div>
 <h1>Loan account Schedule</h1>
 <table width="310" border="0">
 <tr>
 <td width="100">Loan Amount </td>
 <td width="10">&nbsp;</td>
<td width="200"><input type="text" name="loanAmount" id="loanAmount" onBlur="validate(document.aspnetForm.loanAmount)"/></td>
 </tr>
 <tr>
 <td width="100"><label>Rate of Intrest</label></td>
 <td width="10">&nbsp;</td>
 <td width="200"><input type="text" name="rateOfIntrest" id="rateOfIntrest" onBlur="validate(document.aspnetForm.rateOfIntrest)" /></td>
 </tr>
 <tr>
<td width="100">Terms Year </td>
 <td width="10">&nbsp;</td>
 <td width="200"><input type="text" name="termsYear" id="termsYear" onBlur="validate(document.aspnetForm.termsYear)" /></td>
 </tr>
 <tr>
 <td width="100">Total Month</td>
 <td width="10">&nbsp;</td>
 <td width="200"><p id="t_month"></p></td>

 </tr>
 <tr>
 <td width="100">&nbsp;</td>
 <td width="10">&nbsp;</td>
 <td width="200"><a href="#" onClick="calculatePayment()"><img name="Calculate" style="float:left"  width="148" height="22" alt="Calculate" border="0"></a><a href="" onClick="return(clearbox(document.aspnetForm))">   &nbsp; <img name="Clear" style="float:right" width="148"   border="0" alt="Clear"></a></td>
 </tr>
 </table>
 <p id="show" style="font-family:'Trebuchet MS'; font-size:20px; text-align:left; font-weight:bold"></p>

 <div id="details">

 </div>
 <p>&nbsp;</p>
 </div>

 </div>
 <div></div>

 </div>
 </div>

</form>
 </body>
 </html>
<code>

Step-2:
The above code generate the following table

step-3

We need some javascript to calculate the Loan amount:

Open your favorite code editor paste this code,save it as

loan_schedule.js

 function validate(docname)
{
var i;
var getval=docname.value;

// Validate data:- parse string to check whether it contains
// characters other than numbers and one decimal
var count_deci=0;
var flag=0;
// parse through the data
for(i=0;i<getval.length;i=i+1)
{ if((getval.charAt(i) >= "0" && getval.charAt(i)  <= "9") || (getval.charAt(i) == "."))
 {    // if decimal
 if(getval.charAt(i) == ".")
 {
 count_deci=count_deci+1;
 // to check entries of the type X.
 if(getval.charAt(i+1)=="")
 {
 alert("Wrong entry");
 docname.value="";
 docname.focus();
 flag=1;
 break;
 }
 }
 // check if there is more than one decimal
 if(count_deci>1)
 { alert("Illegal Data");
 docname.value="";
 docname.focus();
 flag=1;
 break;
 }
 }
 // executed if data contains alphabets or special chars
 else
 { alert("Invalid Entry");
 docname.value="";
 docname.focus();
 flag=1;
 break;
 }

}
}

function clearbox(docname)
{
var totalrecords;

// obtain the count of records retrieved
totalrecords=3
for(i=0;i<totalrecords;i++)
docname[i].value="";
docname.area0.focus();
return false;
}

<!--
 function calculatePayment() {
 //princ = form.loanAmount.value - form.dp.value;
 //intRate = (form.rateOfIntrest.value/100) / 12;
 var months = document.aspnetForm.termsYear.value * 12;
 var princp=document.aspnetForm.loanAmount.value*1;
 var intr=document.aspnetForm.rateOfIntrest.value*1;
 if (months!=0 && princp!=0 && intr!=0)
 {

 var numerator=princp*Math.pow((1+intr/(12*100)),months);
 var denominator=100*12*(Math.pow((1+intr/(12*100)),months)-1)/intr;
 // var tt = Math.floor((princp*intr)/(1-Math.pow(1+intr,(-1*months)))*100)/100;
 var tt=12*(numerator/(denominator*12));
 ftt=currency(tt);

 document.getElementById('show').innerHTML='Monthly EMI : '+ftt;
 document.getElementById('t_month').innerHTML=months;
 //form.principle.value = princ;
 //form.payments.value = months;
 var details_data="<table width='30%'  style='font-family:tahoma; font-size:11px; border-collapse:collapse; ' cellpadding='0' cellspacing='0'><tr ><td style='background:#EDA55C; color:#ffffff; border:solid 1px #EDA55C; padding:2px'>Month</td><td style='background:#EDA55C; color:#ffffff; border:solid 1px #EDA55C; padding:2px'>Opening Balance</td><td style='background:#EDA55C; color:#ffffff; border:solid 1px #EDA55C; padding:2px'>Monthly EMI</td><td style='background:#EDA55C; color:#ffffff; border:solid 1px #EDA55C; padding:2px'>Intrest</td><td style='background:#EDA55C; color:#ffffff; border:solid 1px #EDA55C; padding:2px'>Princpal</td><td style='background:#EDA55C; color:#ffffff; border:solid 1px #EDA55C; padding:2px'>Closing Balance</td></tr>";
 var dataEMI=0
 var o_balance=princp
 var dataprincp=0
 var dataintr=0
 var c_balance=0
 for(i=1;i<=months;i++)
 {
 dataintr=((o_balance*intr)/12)/100;
 dataprincp=Math.round(tt-dataintr)
 c_balance=o_balance-dataprincp
 details_data+='<tr ><td style="border:solid 1px #EDA55C; padding:2px">'+i+'</td><td style="border:solid 1px #EDA55C; padding:2px">'+o_balance+'</td><td style="border:solid 1px #EDA55C; padding:2px">'+Math.ceil(tt)+'</td><td style="border:solid 1px #EDA55C; padding:2px">'+Math.ceil(dataintr)+'</td><td style="border:solid 1px #EDA55C; padding:2px">'+dataprincp+'</td><td style="border:solid 1px #EDA55C; padding:2px">'+c_balance+'</td></tr>'

 o_balance=c_balance
 }

 document.getElementById('details').innerHTML=details_data+'</table>';

 }else{
 alert('Please fill all the fields')
 }
 }
function currency( num )
{
 var prefix = "Rs ";
 var suffix = "";
 if ( num < 0 )
 {
 prefix = "(Rs ";
 suffix = ")";
 num = - num;
 }
 var temp = Math.round( num * 100.0 ); // convert to pennies!
 if ( temp < 10 ) return prefix + "0.0" + temp + suffix;
 if ( temp < 100 ) return prefix + "0." + temp + suffix;

 temp = prefix + temp; // convert to string!
 var diff=temp.length-5
 var fst=temp.substring(3,temp.length-5)
 var snd=temp.substring(diff)
 var dec=snd.substring(3)
 var bdec=snd.substring(0,3)
 var arr = new Array()
 var nlen=fst.length
 for(i=0;Math.round(nlen/2)>0;i++)
 {

 arr[i]=fst.substring(nlen-2,nlen)
 nlen=(nlen<0)? 2: nlen;
 nlen-=2
 }
 var dis=""
 for(i=arr.length-1;i>=0;i--)
 {
 dis+=arr[i]+','
 }
 var maindis=prefix+dis+bdec+'.'+dec
 //return temp.substring(3,temp.length-2) + "." + temp.substring(temp.length-2) + suffix;

 return maindis;

}
//-->

Final step:
Save the above  as loan_schedule.js

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s