Coupon Code Validator with HTML, CSS & JavaScript
30 DAYS 30 PROJECT CHALLENGE
Day #17
Project Overview
A Coupon Code Validator built using HTML, CSS, and JavaScript that allows users to apply discount codes and see updated pricing instantly.
The project validates coupon inputs, applies percentage-based discounts, and provides clear feedback for valid or invalid codes, simulating real-world e-commerce behavior.
Key Features
- Validates coupon codes entered by the user.
- Applies percentage-based discounts dynamically.
- Instantly updates the final price after applying a coupon.
- Displays clear success and error messages.
- Prevents empty or invalid coupon submissions.
- Clean, responsive, and user-friendly interface.
- Easy to extend with expiry dates or backend validation.
HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Coupon Code Validator</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="coupon-container">
<h2>Apply Coupon</h2>
<div class="price">
Original Price: ₹<span id="originalPrice">1000</span>
</div>
<p class="available">
Available Coupons: <strong>SAVE10</strong>, <strong>SAVE20</strong>, <strong>DISCOUNT50</strong>
</p>
<input
type="text"
id="couponInput"
placeholder="Enter coupon code"
/>
<button onclick="applyCoupon()">Apply Coupon</button>
<p class="message" id="message"></p>
<div class="final-price">
Final Price: ₹<span id="finalPrice">1000</span>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
CSS Code
* {
box-sizing: border-box;
font-family: "Segoe UI", sans-serif;
}
body {
min-height: 100vh;
background: #042453;
display: flex;
justify-content: center;
align-items: center;
}
.coupon-container {
background: #ffffff;
width: 320px;
padding: 25px;
border-radius: 12px;
text-align: center;
box-shadow: 0 15px 30px rgba(0,0,0,0.1);
}
h2 {
margin-bottom: 15px;
}
.price,
.final-price {
margin: 10px 0;
font-weight: 600;
}
input {
width: 100%;
padding: 10px;
border-radius: 6px;
border: 1px solid #ccc;
margin: 10px 0;
}
button {
width: 100%;
padding: 10px;
border: none;
border-radius: 6px;
background: #4f46e5;
color: white;
cursor: pointer;
}
.message {
margin-top: 10px;
font-size: 14px;
}
Javascript Code
const originalPrice = 1000;
const finalPriceEl = document.getElementById("finalPrice");
const message = document.getElementById("message");
const coupons = {
SAVE10: 10,
SAVE20: 20,
DISCOUNT50: 50
};
function applyCoupon() {
alert("Try these coupons: SAVE10, SAVE20, DISCOUNT50");
const input = document.getElementById("couponInput").value.trim().toUpperCase();
if (!input) {
message.style.color = "red";
message.innerText = "Please enter a coupon code";
return;
}
if (coupons[input]) {
const discount = coupons[input];
const discountedPrice = originalPrice - (originalPrice * discount) / 100;
finalPriceEl.innerText = discountedPrice;
message.style.color = "green";
message.innerText = `Coupon applied! You saved ${discount}%`;
} else {
finalPriceEl.innerText = originalPrice;
message.style.color = "red";
message.innerText = "Invalid coupon code";
}
}
Related Projects
Day 15 : Email Subscription Form
Simple email subscription form with validation and feedback messages.
Concepts: Form validation, user input handling, dynamic UI feedback.
Day 19 : Loan EMI Calculator
Calculates monthly loan EMI based on amount, interest rate, and tenure.
Concepts: Mathematical calculations, form handling, dynamic UI updates.
Day 20 : Form Auto-Save (Local Storage)
Automatically saves form input data and restores it on reload.
Concepts: LocalStorage usage, input event handling, persistent UI state.