Understanding how much mortgage you can afford is a crucial first step in the home-buying process. This calculator helps you estimate your potential borrowing power based on your income, debts, and down payment. Remember, this is an estimate, and your actual loan approval will depend on lender-specific criteria, credit score, and market conditions.
.calculator-container {
font-family: Arial, sans-serif;
border: 1px solid #ccc;
padding: 20px;
border-radius: 8px;
max-width: 600px;
margin: 20px auto;
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;
font-size: 0.95em;
}
.calculator-inputs {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 15px;
margin-bottom: 20px;
}
.input-group {
display: flex;
flex-direction: column;
}
.input-group label {
margin-bottom: 5px;
font-weight: bold;
color: #444;
}
.input-group input {
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 1em;
}
.calculator-container button {
display: block;
width: 100%;
padding: 12px 20px;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
font-size: 1.1em;
cursor: pointer;
transition: background-color 0.3s ease;
}
.calculator-container button:hover {
background-color: #0056b3;
}
.calculator-result {
margin-top: 25px;
padding: 15px;
border: 1px solid #e0e0e0;
border-radius: 5px;
background-color: #fff;
text-align: center;
font-size: 1.1em;
color: #333;
min-height: 50px; /* To prevent layout shift */
}
.calculator-result strong {
color: #007bff;
}
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);
var loanTerm = parseFloat(document.getElementById("loanTerm").value);
var resultDiv = document.getElementById("result");
resultDiv.innerHTML = ""; // Clear previous results
// — Input Validation —
if (isNaN(annualIncome) || annualIncome <= 0) {
resultDiv.innerHTML = "Please enter a valid annual income.";
return;
}
if (isNaN(monthlyDebt) || monthlyDebt < 0) {
resultDiv.innerHTML = "Please enter a valid monthly debt amount.";
return;
}
if (isNaN(downPayment) || downPayment < 0) {
resultDiv.innerHTML = "Please enter a valid down payment amount.";
return;
}
if (isNaN(interestRate) || interestRate 20) {
resultDiv.innerHTML = "Please enter a valid interest rate (e.g., 3.5 to 7.0).";
return;
}
if (isNaN(loanTerm) || loanTerm 40) {
resultDiv.innerHTML = "Please enter a valid loan term (e.g., 15 to 30 years).";
return;
}
// — Affordability Calculation —
// Lender's DTI (Debt-to-Income) Ratios often used:
// Front-end ratio (housing costs): typically 28% of gross monthly income
// Back-end ratio (total debt): typically 36% of gross monthly income
// These are general guidelines; actual ratios vary by lender and loan type.
var maxHousingRatio = 0.28; // 28%
var maxTotalDebtRatio = 0.36; // 36%
var grossMonthlyIncome = annualIncome / 12;
var maxMonthlyHousingPayment = grossMonthlyIncome * maxHousingRatio;
var maxTotalMonthlyDebtObligation = grossMonthlyIncome * maxTotalDebtRatio;
// Calculate how much room is left for mortgage P&I (Principal & Interest)
var maxMonthlyMortgagePI = maxTotalMonthlyDebtObligation – monthlyDebt;
// Ensure maxMonthlyMortgagePI is not negative (meaning existing debt is too high for the ratios)
if (maxMonthlyMortgagePI < 0) {
resultDiv.innerHTML = "Based on your income and existing debts, your current debt-to-income ratio may be too high to qualify for a new mortgage.Maximum Affordable Loan Amount: $0";
return;
}
// Determine the limiting factor: housing ratio or total debt ratio
var actualMaxMonthlyMortgagePI = Math.min(maxMonthlyHousingPayment, maxMonthlyMortgagePI);
// — Calculate Maximum Loan Amount based on monthly payment —
// Formula for monthly mortgage payment: M = P [ i(1 + i)^n ] / [ (1 + i)^n – 1]
// Where:
// M = Monthly Payment (actualMaxMonthlyMortgagePI)
// P = Principal Loan Amount (what we want to find)
// i = monthly interest rate (interestRate / 100) / 12
// n = total number of payments (loanTerm * 12)
var monthlyInterestRate = (interestRate / 100) / 12;
var numberOfPayments = loanTerm * 12;
var maxLoanAmount = 0;
if (monthlyInterestRate > 0 && numberOfPayments > 0) {
var factor = Math.pow(1 + monthlyInterestRate, numberOfPayments);
var principalFactor = (factor – 1) / (monthlyInterestRate * factor);
maxLoanAmount = actualMaxMonthlyMortgagePI * principalFactor;
} else if (actualMaxMonthlyMortgagePI > 0) {
// Handle zero interest rate (unlikely but for completeness) – loan amount is essentially total payments
maxLoanAmount = actualMaxMonthlyMortgagePI * numberOfPayments;
}
// The calculated maxLoanAmount is the *principal* you can borrow.
// The total *home price* you can afford is this loan amount plus your down payment.
var affordableHomePrice = maxLoanAmount + downPayment;
// — Display Results —
var formattedAffordableHomePrice = affordableHomePrice.toLocaleString(undefined, {
style: 'currency',
currency: 'USD'
});
var formattedMaxLoanAmount = maxLoanAmount.toLocaleString(undefined, {
style: 'currency',
currency: 'USD'
});
var formattedMaxMonthlyHousing = maxMonthlyHousingPayment.toLocaleString(undefined, {
style: 'currency',
currency: 'USD'
});
var formattedMaxTotalDebt = maxTotalMonthlyDebtObligation.toLocaleString(undefined, {
style: 'currency',
currency: 'USD'
});
var formattedMaxMonthlyMortgagePI = actualMaxMonthlyMortgagePI.toLocaleString(undefined, {
style: 'currency',
currency: 'USD'
});
resultDiv.innerHTML =
"Estimated Maximum Affordable Home Price: " + formattedAffordableHomePrice + "" +
"Estimated Maximum Mortgage Loan Amount: " + formattedMaxLoanAmount + "" +
"(This is the principal loan amount, excluding your down payment)" +
"Breakdown based on lender guidelines (approximate):" +
"Gross Monthly Income: " + (grossMonthlyIncome).toLocaleString(undefined, { style: 'currency', currency: 'USD' }) + "" +
"Maximum Monthly Housing Payment (PITI*): " + formattedMaxMonthlyHousing + "" +
"Maximum Total Monthly Debt (including housing): " + formattedTotalDebt.toLocaleString(undefined, { style: 'currency', currency: 'USD' }) + "" +
"Estimated Available for Mortgage P&I: " + formattedMaxMonthlyMortgagePI + "" +
"*Note: PITI includes Principal, Interest, Taxes, and Insurance. This calculator primarily estimates Principal & Interest based on your inputs. Property taxes and homeowners insurance will vary and will increase your actual total monthly housing cost.";
}