Mortgage Affordability Calculator
This calculator helps you estimate the maximum mortgage you can afford based on your income, debts, and down payment. It's a crucial first step in understanding your home buying budget.
function calculateMortgageAffordability() {
var annualIncome = parseFloat(document.getElementById("annualIncome").value);
var monthlyDebt = parseFloat(document.getElementById("monthlyDebt").value);
var downPayment = parseFloat(document.getElementById("downPayment").value);
var interestRate = parseFloat(document.getElementById("interestRate").value) / 100;
var loanTerm = parseFloat(document.getElementById("loanTerm").value);
var propertyTaxRate = parseFloat(document.getElementById("propertyTaxRate").value) / 100;
var homeInsurance = parseFloat(document.getElementById("homeInsurance").value);
var pmi = parseFloat(document.getElementById("pmi").value);
var resultElement = document.getElementById("result");
resultElement.innerHTML = ""; // Clear previous results
// — Input Validation —
if (isNaN(annualIncome) || annualIncome <= 0) {
resultElement.innerHTML = "Please enter a valid annual gross income.";
return;
}
if (isNaN(monthlyDebt) || monthlyDebt < 0) {
resultElement.innerHTML = "Please enter a valid total monthly debt payment.";
return;
}
if (isNaN(downPayment) || downPayment < 0) {
resultElement.innerHTML = "Please enter a valid down payment amount.";
return;
}
if (isNaN(interestRate) || interestRate <= 0) {
resultElement.innerHTML = "Please enter a valid interest rate.";
return;
}
if (isNaN(loanTerm) || loanTerm <= 0) {
resultElement.innerHTML = "Please enter a valid loan term.";
return;
}
if (isNaN(propertyTaxRate) || propertyTaxRate < 0) {
resultElement.innerHTML = "Please enter a valid property tax rate.";
return;
}
if (isNaN(homeInsurance) || homeInsurance < 0) {
resultElement.innerHTML = "Please enter a valid homeowner's insurance amount.";
return;
}
if (isNaN(pmi) || pmi < 0) {
resultElement.innerHTML = "Please enter a valid PMI amount.";
return;
}
// — Affordability Calculation Logic —
// Lender typically use Debt-to-Income (DTI) ratios. Common front-end DTI is 28% and back-end is 36%.
// We'll use a simplified approach here, focusing on what a borrower can realistically afford for PITI.
var maxMonthlyHousingPayment = (annualIncome / 12) * 0.36; // Assuming a 36% DTI for total debt (including housing)
var allowedMonthlyDebtService = maxMonthlyHousingPayment – monthlyDebt; // The maximum amount left for PITI
if (allowedMonthlyDebtService <= 0) {
resultElement.innerHTML = "Based on your income and existing debts, it appears difficult to afford additional housing costs.";
return;
}
// Estimate the maximum loan amount. This is iterative because property tax and insurance depend on home value.
// We'll make an initial guess for the home value, and refine it.
var estimatedHomeValue = (downPayment / 0.2) + 100000; // Initial guess: down payment is 20%, plus some loan amount
var maxLoanAmount = 0;
var iterations = 0;
var maxIterations = 100;
var tolerance = 0.1;
while (iterations < maxIterations) {
var annualPropertyTax = estimatedHomeValue * propertyTaxRate;
var monthlyPropertyTax = annualPropertyTax / 12;
var monthlyHomeInsurance = homeInsurance / 12;
var monthlyPMI = pmi / 12;
var totalMonthlyHousingCostsExcludingPrincipalInterest = monthlyPropertyTax + monthlyHomeInsurance + monthlyPMI;
var availableForPrincipalInterest = allowedMonthlyDebtService – totalMonthlyHousingCostsExcludingPrincipalInterest;
if (availableForPrincipalInterest 0) {
calculatedLoanAmount = availableForPrincipalInterest * (1 – Math.pow(1 + monthlyInterestRate, -numberOfPayments)) / monthlyInterestRate;
} else {
calculatedLoanAmount = availableForPrincipalInterest * numberOfPayments; // Simple interest if rate is 0
}
var currentHomeValueEstimate = calculatedLoanAmount + downPayment;
var difference = Math.abs(currentHomeValueEstimate – estimatedHomeValue);
if (difference < tolerance) {
maxLoanAmount = calculatedLoanAmount;
estimatedHomeValue = currentHomeValueEstimate; // Final estimated home value
break;
} else {
estimatedHomeValue = currentHomeValueEstimate;
iterations++;
}
}
if (iterations === maxIterations) {
// If the loop finishes without converging, use the last calculated values
maxLoanAmount = estimatedHomeValue – downPayment;
if (maxLoanAmount < 0) maxLoanAmount = 0; // Ensure loan amount is not negative
}
var maxAffordableHomePrice = maxLoanAmount + downPayment;
// — Display Results —
resultElement.innerHTML += "
Estimated Affordability
";
resultElement.innerHTML += "Based on your inputs, you may be able to afford a home priced around:
$" + maxAffordableHomePrice.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,') + "";
resultElement.innerHTML += "This includes an estimated maximum loan amount of:
$" + maxLoanAmount.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,') + "";
// Calculate estimated monthly PITI for the affordable home
var annualPropertyTaxEstimate = maxAffordableHomePrice * propertyTaxRate;
var monthlyPropertyTaxEstimate = annualPropertyTaxEstimate / 12;
var monthlyHomeInsuranceEstimate = homeInsurance / 12;
var monthlyPMIEstimate = pmi / 12;
var monthlyInterestRate = interestRate / 12;
var numberOfPayments = loanTerm * 12;
var monthlyPrincipalInterest = 0;
if (monthlyInterestRate > 0) {
monthlyPrincipalInterest = maxLoanAmount * (monthlyInterestRate * Math.pow(1 + monthlyInterestRate, numberOfPayments)) / (Math.pow(1 + monthlyInterestRate, numberOfPayments) – 1);
} else {
monthlyPrincipalInterest = maxLoanAmount / numberOfPayments;
}
var estimatedMonthlyPITI = monthlyPrincipalInterest + monthlyPropertyTaxEstimate + monthlyHomeInsuranceEstimate + monthlyPMIEstimate;
resultElement.innerHTML += "Estimated monthly mortgage payment (Principal, Interest, Taxes, Insurance, PMI):
$" + estimatedMonthlyPITI.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,') + "";
// Calculate resulting DTI
var totalMonthlyObligations = monthlyDebt + estimatedMonthlyPITI;
var calculatedDTI = (totalMonthlyObligations / (annualIncome / 12)) * 100;
resultElement.innerHTML += "Estimated total DTI ratio:
" + calculatedDTI.toFixed(1) + "%";
resultElement.innerHTML += "
Disclaimer: This is an estimate. Actual affordability may vary based on lender specific criteria, credit score, market conditions, and other factors. It is recommended to consult with a mortgage professional.";
}
.calculator-container {
font-family: sans-serif;
max-width: 700px;
margin: 20px auto;
padding: 20px;
border: 1px solid #e0e0e0;
border-radius: 8px;
background-color: #f9f9f9;
}
.calculator-container h2 {
text-align: center;
color: #333;
margin-bottom: 15px;
}
.calculator-container p {
color: #555;
line-height: 1.6;
margin-bottom: 20px;
text-align: justify;
}
.calculator-form {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 15px;
margin-bottom: 20px;
}
.form-group {
display: flex;
flex-direction: column;
}
.form-group label {
margin-bottom: 5px;
font-weight: bold;
color: #444;
}
.form-group input[type="number"] {
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 16px;
box-sizing: border-box; /* Include padding and border in the element's total width and height */
}
.form-group input[type="number"]:focus {
outline: none;
border-color: #007bff;
box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.25);
}
.calculator-form button {
grid-column: 1 / -1; /* Span across all columns */
padding: 12px 20px;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
font-size: 18px;
cursor: pointer;
transition: background-color 0.3s ease;
}
.calculator-form button:hover {
background-color: #0056b3;
}
.calculator-result {
margin-top: 20px;
padding: 15px;
border: 1px dashed #007bff;
border-radius: 5px;
background-color: #e7f3ff;
text-align: center;
}
.calculator-result h3 {
color: #0056b3;
margin-bottom: 10px;
}
.calculator-result p {
margin-bottom: 8px;
color: #333;
}
.calculator-result strong {
color: #007bff;
}
.calculator-result small {
color: #666;
font-style: italic;
}