<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Credit Card Expiration Date Validator</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
.container {
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
text-align: center;
}
input {
padding: 10px;
margin: 10px 0;
width: 100px;
}
button {
padding: 10px 20px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
#result {
margin-top: 20px;
font-weight: bold;
}
</style>
</head>
<body>
<div class="container">
<h2>Credit Card Expiration Date Validator</h2>
<p>Enter expiration date in MM/YY format (e.g., 12/25)</p>
<input type="text" id="expDate" placeholder="MM/YY">
<button onclick="validateExpDate()">Validate</button>
<div id="result"></div>
</div>
<script>
function validateExpDate() {
const expDate = document.getElementById('expDate').value;
const resultDiv = document.getElementById('result');
const dateRegex = /^(0[1-9]|1[0-2])\/([0-9]{2})$/;
if (!dateRegex.test(expDate)) {
resultDiv.textContent = "Invalid format! Please use MM/YY (e.g., 12/25).";
resultDiv.style.color = "red";
return;
}
const [month, year] = expDate.split('/').map(Number);
const fullYear = 2000 + year;
const currentDate = new Date();
currentDate.setHours(0, 0, 0, 0); // Reset time for comparison
const expDateObj = new Date(fullYear, month, 0); // Last day of the month
if (month < 1 || month > 12) {
resultDiv.textContent = "Invalid month! Must be between 01 and 12.";
resultDiv.style.color = "red";
return;
}
if (expDateObj < currentDate) {
resultDiv.textContent = "This card has expired!";
resultDiv.style.color = "red";
} else {
resultDiv.textContent = "This expiration date is valid!";
resultDiv.style.color = "green";
}
}
</script>
</body>
</html>