Your Estimated Mortgage Affordability:
function calculateMortgageAffordability() {
var annualIncome = parseFloat(document.getElementById("annualIncome").value);
var monthlyDebtPayments = parseFloat(document.getElementById("monthlyDebtPayments").value);
var downPayment = parseFloat(document.getElementById("downPayment").value);
var interestRate = parseFloat(document.getElementById("interestRate").value);
var loanTerm = parseFloat(document.getElementById("loanTerm").value);
var resultDiv = document.getElementById("result");
resultDiv.innerHTML = ""; // Clear previous results
if (isNaN(annualIncome) || isNaN(monthlyDebtPayments) || isNaN(downPayment) || isNaN(interestRate) || isNaN(loanTerm)) {
resultDiv.innerHTML = "Please enter valid numbers for all fields.";
return;
}
if (annualIncome <= 0 || monthlyDebtPayments < 0 || downPayment < 0 || interestRate <= 0 || loanTerm <= 0) {
resultDiv.innerHTML = "Please enter positive values where appropriate.";
return;
}
// General rule of thumb: Debt-to-Income (DTI) ratio should not exceed 43%
// Max monthly housing payment = (Annual Income * 0.43) – Monthly Debt Payments
var maxMonthlyHousingPayment = (annualIncome * 0.43) – monthlyDebtPayments;
if (maxMonthlyHousingPayment 0 && numberOfPayments > 0) {
maxLoanAmount = maxMonthlyHousingPayment * (Math.pow(1 + monthlyInterestRate, numberOfPayments) – 1) / (monthlyInterestRate * Math.pow(1 + monthlyInterestRate, numberOfPayments));
}
var totalAffordability = maxLoanAmount + downPayment;
resultDiv.innerHTML = "Maximum estimated monthly housing payment (Principal, Interest, Taxes, Insurance):
$" + maxMonthlyHousingPayment.toFixed(2) + "" +
"Estimated maximum loan amount:
$" + maxLoanAmount.toFixed(2) + "" +
"Estimated total home purchase affordability (Loan + Down Payment):
$" + totalAffordability.toFixed(2) + "" +
"
Note: This is an estimation. Actual loan approval depends on lender criteria and a full financial review.";
}
.calculator-wrapper {
font-family: sans-serif;
max-width: 600px;
margin: 20px auto;
padding: 20px;
border: 1px solid #ddd;
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.calculator-form h2,
.calculator-result h3 {
text-align: center;
margin-bottom: 20px;
color: #333;
}
.calculator-form p {
margin-bottom: 25px;
color: #555;
line-height: 1.6;
}
.form-group {
margin-bottom: 15px;
}
.form-group label {
display: block;
margin-bottom: 5px;
font-weight: bold;
color: #444;
}
.form-group input[type="number"] {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box; /* Include padding and border in the element's total width and height */
}
button {
display: block;
width: 100%;
padding: 12px 15px;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
font-size: 16px;
cursor: pointer;
transition: background-color 0.3s ease;
margin-top: 20px;
}
button:hover {
background-color: #0056b3;
}
.calculator-result {
margin-top: 30px;
padding-top: 20px;
border-top: 1px solid #eee;
}
#result p {
margin-bottom: 10px;
color: #333;
line-height: 1.5;
}
#result strong {
color: #28a745;
}
#result small {
font-size: 0.85em;
color: #777;
display: block;
margin-top: 10px;
}