Mortgage Affordability Calculator
Understanding how much you can afford for a mortgage is a crucial first step in the home-buying process. This calculator helps you estimate your maximum affordable mortgage payment based on your income, debts, and estimated interest rates.
.calculator-container {
font-family: sans-serif;
max-width: 600px;
margin: 20px auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 8px;
background-color: #f9f9f9;
}
.calculator-container h2 {
text-align: center;
color: #333;
margin-bottom: 20px;
}
.calculator-form .form-group {
margin-bottom: 15px;
}
.calculator-form label {
display: block;
margin-bottom: 5px;
font-weight: bold;
color: #555;
}
.calculator-form input[type="number"],
.calculator-form input[type="text"] {
width: calc(100% – 20px); /* Adjust for padding */
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box; /* Include padding and border in the element's total width and height */
}
.calculator-form button {
width: 100%;
padding: 12px 15px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
transition: background-color 0.3s ease;
}
.calculator-form button:hover {
background-color: #45a049;
}
.calculator-result {
margin-top: 25px;
padding: 15px;
background-color: #e8f5e9;
border: 1px solid #c8e6c9;
border-radius: 4px;
text-align: center;
font-size: 1.1em;
color: #2e7d32;
}
.calculator-result p {
margin: 5px 0;
}
function calculateAffordability() {
var monthlyIncome = parseFloat(document.getElementById("monthlyIncome").value);
var monthlyDebt = parseFloat(document.getElementById("monthlyDebt").value);
var downPayment = parseFloat(document.getElementById("downPayment").value);
var interestRate = parseFloat(document.getElementById("interestRate").value);
var loanTerm = parseFloat(document.getElementById("loanTerm").value);
var propertyTax = parseFloat(document.getElementById("propertyTax").value);
var homeInsurance = parseFloat(document.getElementById("homeInsurance").value);
var pmi = parseFloat(document.getElementById("pmi").value);
var resultDiv = document.getElementById("result");
resultDiv.innerHTML = ""; // Clear previous results
// Input validation
if (isNaN(monthlyIncome) || monthlyIncome <= 0 ||
isNaN(monthlyDebt) || monthlyDebt < 0 ||
isNaN(downPayment) || downPayment < 0 ||
isNaN(interestRate) || interestRate <= 0 ||
isNaN(loanTerm) || loanTerm <= 0 ||
isNaN(propertyTax) || propertyTax < 0 ||
isNaN(homeInsurance) || homeInsurance < 0 ||
isNaN(pmi) || pmi < 0) {
resultDiv.innerHTML = "Please enter valid positive numbers for all fields.";
return;
}
// Lender Debt-to-Income Ratio (DTI) guidelines (common starting point)
// Front-end DTI (housing costs) often around 28-31% of gross income
// Back-end DTI (total debt) often around 36-43% of gross income
var maxHousingCostRatio = 0.31; // Example: 31% for PITI
var maxTotalDebtRatio = 0.43; // Example: 43% for PITI + other debts
var maxMonthlyHousingPayment = monthlyIncome * maxHousingCostRatio;
var maxTotalMonthlyDebt = monthlyIncome * maxTotalDebtRatio;
// Calculate the maximum allowable PITI (Principal, Interest, Taxes, Insurance)
var maxPITI = maxTotalMonthlyDebt – monthlyDebt;
if (maxPITI maxMonthlyHousingPayment) {
maxPITI = maxMonthlyHousingPayment;
}
var monthlyPropertyTax = propertyTax / 12;
var monthlyHomeInsurance = homeInsurance / 12;
var monthlyPMI = pmi / 12;
var monthlyPITI = maxPITI;
var monthlyInterestPaymentAllowed = monthlyPITI – monthlyPropertyTax – monthlyHomeInsurance – monthlyPMI;
if (monthlyInterestPaymentAllowed 0 && numberOfPayments > 0) {
// Mortgage Payment Formula: M = P [ i(1 + i)^n ] / [ (1 + i)^n – 1]
// We need to solve for P (Principal Loan Amount):
// P = M [ (1 + i)^n – 1] / [ i(1 + i)^n ]
var numerator = Math.pow(1 + monthlyInterestRate, numberOfPayments) – 1;
var denominator = monthlyInterestRate * Math.pow(1 + monthlyInterestRate, numberOfPayments);
maxLoanAmount = monthlyInterestPaymentAllowed * (numerator / denominator);
}
var affordableHousePrice = maxLoanAmount + downPayment;
resultDiv.innerHTML =
"Estimated Maximum Affordable House Price:
$" + affordableHousePrice.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ",") + "" +
"Estimated Maximum Loan Amount:
$" + maxLoanAmount.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ",") + "" +
"Estimated Maximum Monthly PITI:
$" + maxPITI.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ",") + "";
}