Understanding how much you can afford to borrow for a mortgage is a crucial first step in the home-buying process. This calculator helps you estimate your maximum affordable mortgage amount based on your income, debts, and desired loan terms. Remember, this is an estimate, and your actual borrowing capacity will depend on the lender's specific criteria.
.calculator-container {
font-family: sans-serif;
max-width: 600px;
margin: 20px auto;
padding: 20px;
border: 1px solid #ddd;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.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: #333;
}
.input-group input {
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 1rem;
}
.calculator-container button {
background-color: #4CAF50;
color: white;
padding: 12px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 1.1rem;
transition: background-color 0.3s ease;
}
.calculator-container button:hover {
background-color: #45a049;
}
.calculator-result {
margin-top: 20px;
padding: 15px;
background-color: #e7f3fe;
border: 1px solid #cce5ff;
border-radius: 4px;
font-size: 1.1rem;
color: #004085;
text-align: center;
}
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 resultElement = document.getElementById("result");
// Basic validation
if (isNaN(annualIncome) || isNaN(monthlyDebt) || isNaN(downPayment) || isNaN(interestRate) || isNaN(loanTerm) ||
annualIncome <= 0 || monthlyDebt < 0 || downPayment < 0 || interestRate <= 0 || loanTerm <= 0) {
resultElement.innerHTML = "Please enter valid positive numbers for all fields.";
return;
}
// Lender often use Debt-to-Income (DTI) ratio. A common guideline is that total housing costs (PITI: Principal, Interest, Taxes, Insurance)
// plus other monthly debts should not exceed 36-43% of gross monthly income.
// Let's use a DTI limit of 43% for this calculation as a common example.
var maxDTI = 0.43;
var grossMonthlyIncome = annualIncome / 12;
var maxTotalMonthlyHousingPayment = (grossMonthlyIncome * maxDTI) – monthlyDebt;
if (maxTotalMonthlyHousingPayment <= 0) {
resultElement.innerHTML = "Based on your income and existing debts, your maximum affordable monthly housing payment is too low to qualify for a mortgage. Consider increasing income or reducing debt.";
return;
}
// Estimate property taxes and homeowner's insurance as a percentage of the home value.
// This is a simplification; actual costs vary significantly by location and property.
// Let's assume 1.2% of home value annually for taxes and 0.5% for insurance.
var annualTaxRate = 0.012;
var annualInsuranceRate = 0.005;
var monthlyTaxInsuranceEstimate = 0; // This will be calculated iteratively or approximated.
// We need to find the maximum loan amount (Loan_Amount) such that the monthly payment (P&I)
// plus estimated monthly taxes and insurance is less than or equal to maxTotalMonthlyHousingPayment.
// P&I = P * [r(1+r)^n] / [(1+r)^n – 1]
// Where:
// P = Principal loan amount
// r = monthly interest rate (annualRate / 12)
// n = total number of payments (loanTerm * 12)
var monthlyInterestRate = (interestRate / 100) / 12;
var numberOfPayments = loanTerm * 12;
// We can't directly solve for P because taxes and insurance depend on the home price, which is P + downPayment.
// We'll use an iterative approach or a simplified calculation.
// Let's try to estimate the maximum loan amount first, assuming taxes/insurance are a portion of the loan.
// This is an approximation. A more precise calculation would involve solving for the home price.
// Simplified: Assume taxes and insurance are a fixed percentage of the *loan amount* for estimation purposes.
// This is inaccurate but provides a ballpark. A better approach is iterative.
// For simplicity here, let's assume we are calculating the maximum *loan amount* first.
// Let's rephrase: What is the maximum P (Principal) such that P&I + estimated T&I <= maxTotalMonthlyHousingPayment?
// For estimation, let's assume T&I is roughly 1% of the loan amount per month. (This is a very rough guess).
// monthly_payment_estimate = P * (monthlyInterestRate * Math.pow(1 + monthlyInterestRate, numberOfPayments)) / (Math.pow(1 + monthlyInterestRate, numberOfPayments) – 1);
// maxTotalMonthlyHousingPayment = monthly_payment_estimate + (Loan_Amount * 0.01) // Rough T&I estimate
// A more accurate method is to solve for the loan amount iteratively or by understanding that the home price is `Loan_Amount + Down_Payment`.
// var `H` be the home price. `H = P + DP`.
// Max Monthly Payment = P&I(P) + T&I(H)
// Max Monthly Payment = P&I(P) + (annualTaxRate * H / 12) + (annualInsuranceRate * H / 12)
// Max Monthly Payment = P&I(P) + ((annualTaxRate + annualInsuranceRate) * (P + DP) / 12)
// We can estimate by finding the P&I payment first, then checking if remaining funds can cover T&I.
// Let's assume maxTotalMonthlyHousingPayment is for P&I only, and then subtract an estimate for T&I to get the max P&I.
// This is still an approximation as T&I scales with home value.
// Let's try a direct calculation for maximum loan amount based on max P&I, then adjust.
// Max P&I = maxTotalMonthlyHousingPayment – estimated_monthly_taxes_and_insurance
// The problem is T&I depends on the total home price (Loan + Down Payment).
// Let's use an iterative approach to find the maximum affordable loan amount.
var maxLoanAmount = 0;
var lowerBound = 0;
var upperBound = annualIncome * 10; // A generous upper bound for the loan amount
var iterations = 0;
var maxIterations = 100;
while (iterations < maxIterations) {
var midLoanAmount = (lowerBound + upperBound) / 2;
var estimatedHomePrice = midLoanAmount + downPayment;
// Calculate estimated monthly taxes and insurance based on estimated home price
var estimatedMonthlyTaxes = (annualTaxRate * estimatedHomePrice) / 12;
var estimatedMonthlyInsurance = (annualInsuranceRate * estimatedHomePrice) / 12;
var estimatedTotalMonthlyTaxesAndInsurance = estimatedMonthlyTaxes + estimatedMonthlyInsurance;
// Calculate the maximum principal & interest payment affordable
var maxAffordablePI = maxTotalMonthlyHousingPayment – estimatedTotalMonthlyTaxesAndInsurance;
if (maxAffordablePI 0) {
calculatedPrincipal = maxAffordablePI * (Math.pow(1 + monthlyInterestRate, numberOfPayments) – 1) / (monthlyInterestRate * Math.pow(1 + monthlyInterestRate, numberOfPayments));
} else { // Handle 0% interest rate, though unlikely for mortgages
calculatedPrincipal = maxAffordablePI * numberOfPayments;
}
if (Math.abs(calculatedPrincipal – midLoanAmount) midLoanAmount) {
// The midLoanAmount is too low, we can afford more principal.
lowerBound = midLoanAmount;
} else {
// The midLoanAmount is too high, we can only afford less principal.
upperBound = midLoanAmount;
}
iterations++;
}
// If loop finished without breaking, use the last calculated 'calculatedPrincipal' if it's higher and within bounds
if (iterations === maxIterations) {
// Re-calculate based on the final lowerBound/upperBound to get a final estimate
var finalEstimatedHomePrice = lowerBound + downPayment;
var finalEstimatedMonthlyTaxes = (annualTaxRate * finalEstimatedHomePrice) / 12;
var finalEstimatedMonthlyInsurance = (annualInsuranceRate * finalEstimatedHomePrice) / 12;
var finalEstimatedTotalMonthlyTaxesAndInsurance = finalEstimatedMonthlyTaxes + finalEstimatedMonthlyInsurance;
var finalMaxAffordablePI = maxTotalMonthlyHousingPayment – finalEstimatedTotalMonthlyTaxesAndInsurance;
if (finalMaxAffordablePI > 0 && monthlyInterestRate > 0) {
maxLoanAmount = finalMaxAffordablePI * (Math.pow(1 + monthlyInterestRate, numberOfPayments) – 1) / (monthlyInterestRate * Math.pow(1 + monthlyInterestRate, numberOfPayments));
} else if (finalMaxAffordablePI > 0) {
maxLoanAmount = finalMaxAffordablePI * numberOfPayments;
} else {
maxLoanAmount = 0; // Cannot afford even T&I
}
}
if (maxLoanAmount <= 0) {
resultElement.innerHTML = "Based on the provided details, your estimated maximum affordable mortgage loan amount is $0. Please review your income, debts, and the estimated interest rate and loan term.";
} else {
var affordableHomePrice = maxLoanAmount + downPayment;
// Format numbers for display
var formattedMaxLoanAmount = maxLoanAmount.toLocaleString(undefined, { style: 'currency', currency: 'USD' });
var formattedAffordableHomePrice = affordableHomePrice.toLocaleString(undefined, { style: 'currency', currency: 'USD' });
var formattedMaxTotalMonthlyHousingPayment = maxTotalMonthlyHousingPayment.toLocaleString(undefined, { style: 'currency', currency: 'USD' });
resultElement.innerHTML =
"Estimated Maximum Affordable Mortgage Loan Amount: " + formattedMaxLoanAmount + "" +
"Estimated Maximum Affordable Home Price (including down payment): " + formattedAffordableHomePrice + "" +
"Estimated Maximum Total Monthly Housing Payment (PITI): " + formattedMaxTotalMonthlyHousingPayment + "" +
"Note: This is an estimate based on a 43% Debt-to-Income ratio and assumed annual property tax (1.2%) and homeowner's insurance (0.5%) rates. Actual loan approval and amounts may vary.";
}
}