This calculator helps you estimate the maximum mortgage amount you can afford based on your income, debts, and desired monthly payment. It's important to remember that this is an estimate, and your actual borrowing capacity may vary depending on the lender's specific criteria, credit score, and market conditions.
.calculator-container {
font-family: sans-serif;
padding: 20px;
border: 1px solid #ccc;
border-radius: 8px;
max-width: 600px;
margin: 20px auto;
background-color: #f9f9f9;
}
.calculator-container h2 {
text-align: center;
color: #333;
margin-bottom: 20px;
}
.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: #555;
}
.input-group input {
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 16px;
}
.calculator-container button {
display: block;
width: 100%;
padding: 12px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
font-size: 18px;
cursor: pointer;
transition: background-color 0.3s ease;
margin-top: 10px;
}
.calculator-container button:hover {
background-color: #45a049;
}
.calculator-result {
margin-top: 25px;
padding: 15px;
background-color: #e0f7fa;
border: 1px solid #00bcd4;
border-radius: 4px;
font-size: 18px;
text-align: center;
color: #00796b;
font-weight: bold;
}
.calculator-result span {
font-weight: normal;
color: #333;
}
function calculateMortgageAffordability() {
var grossMonthlyIncome = parseFloat(document.getElementById("grossMonthlyIncome").value);
var estimatedMonthlyPropertyTaxes = parseFloat(document.getElementById("estimatedMonthlyPropertyTaxes").value);
var estimatedMonthlyHomeInsurance = parseFloat(document.getElementById("estimatedMonthlyHomeInsurance").value);
var existingMonthlyDebt = parseFloat(document.getElementById("existingMonthlyDebt").value);
var desiredMonthlyMortgagePayment = parseFloat(document.getElementById("desiredMonthlyMortgagePayment").value);
var annualInterestRate = parseFloat(document.getElementById("annualInterestRate").value);
var loanTermYears = parseFloat(document.getElementById("loanTermYears").value);
var resultDiv = document.getElementById("result");
resultDiv.innerHTML = ""; // Clear previous results
// — Input Validation —
if (isNaN(grossMonthlyIncome) || grossMonthlyIncome <= 0 ||
isNaN(estimatedMonthlyPropertyTaxes) || estimatedMonthlyPropertyTaxes < 0 ||
isNaN(estimatedMonthlyHomeInsurance) || estimatedMonthlyHomeInsurance < 0 ||
isNaN(existingMonthlyDebt) || existingMonthlyDebt < 0 ||
isNaN(desiredMonthlyMortgagePayment) || desiredMonthlyMortgagePayment < 0 ||
isNaN(annualInterestRate) || annualInterestRate <= 0 ||
isNaN(loanTermYears) || loanTermYears affordablePITI) {
// If the desired payment (with taxes/insurance) is too high, we need to reduce the target P&I
// Or, indicate that the desired payment is unaffordable.
// For this calculator, we'll focus on finding the maximum loan amount based on the desired P&I as a *part* of PITI.
// Let's recalculate affordability to find the max P&I the user can afford given their desired total PITI.
// Calculate how much of the affordablePITI is left for P&I
var affordableP_and_I = affordablePITI – estimatedMonthlyPropertyTaxes – estimatedMonthlyHomeInsurance;
if (affordableP_and_I <= 0) {
resultDiv.innerHTML = "Based on your income and existing debts, you may not be able to afford a mortgage payment (Principal & Interest) after taxes and insurance. Consider increasing income or reducing debt.";
return;
}
// Now we use the affordableP_and_I to calculate the maximum loan amount
var monthlyInterestRate = (annualInterestRate / 100) / 12;
var numberOfMonths = loanTermYears * 12;
if (monthlyInterestRate <= 0) {
resultDiv.innerHTML = "Loan amount calculation requires a positive interest rate.";
return;
}
// Mortgage payment formula: M = P [ i(1 + i)^n ] / [ (1 + i)^n – 1]
// Where: M = Monthly Payment, P = Principal Loan Amount, i = Monthly Interest Rate, n = Number of Months
// We need to solve for P: P = M [ (1 + i)^n – 1] / [ i(1 + i)^n ]
var pow_n_plus_1 = Math.pow(1 + monthlyInterestRate, numberOfMonths);
var principalLoanAmount = affordableP_and_I * (pow_n_plus_1 – 1) / (monthlyInterestRate * pow_n_plus_1);
resultDiv.innerHTML = "Your estimated maximum loan amount is: $" + principalLoanAmount.toFixed(2) + "";
resultDiv.innerHTML += "This is based on a maximum P&I payment of $" + affordableP_and_I.toFixed(2) + " per month.";
resultDiv.innerHTML += "Total estimated monthly housing payment (PITI): $" + (affordableP_and_I + estimatedMonthlyPropertyTaxes + estimatedMonthlyHomeInsurance).toFixed(2) + "";
} else {
// The user's desired P&I payment fits within the affordability limits.
// We can now calculate the maximum loan amount for that specific desired P&I payment.
var monthlyInterestRate = (annualInterestRate / 100) / 12;
var numberOfMonths = loanTermYears * 12;
if (monthlyInterestRate <= 0) {
resultDiv.innerHTML = "Loan amount calculation requires a positive interest rate.";
return;
}
// Mortgage payment formula: M = P [ i(1 + i)^n ] / [ (1 + i)^n – 1]
// Solve for P: P = M [ (1 + i)^n – 1] / [ i(1 + i)^n ]
var pow_n_plus_1 = Math.pow(1 + monthlyInterestRate, numberOfMonths);
var principalLoanAmount = desiredMonthlyMortgagePayment * (pow_n_plus_1 – 1) / (monthlyInterestRate * pow_n_plus_1);
resultDiv.innerHTML = "Your estimated maximum loan amount for a desired P&I payment of $" + desiredMonthlyMortgagePayment.toFixed(2) + " is: $" + principalLoanAmount.toFixed(2) + "";
resultDiv.innerHTML += "Total estimated monthly housing payment (PITI): $" + (desiredMonthlyMortgagePayment + estimatedMonthlyPropertyTaxes + estimatedMonthlyHomeInsurance).toFixed(2) + "";
}
}