Small utility projects are a great way to practise JavaScript, especially when they check real-world data. In this tutorial we will build a spice label checker: you type in the details printed on a pack, and the tool tells you what looks incomplete or suspicious.
Why spices? Because people who search for the best spices online often have no way to compare packs except by reading the label. A few simple rules, such as a full ingredient list and sensible dates, catch many problems before you order. The same idea works for any packaged food, so you can reuse the code.
No framework, no build step. Just HTML and plain JavaScript.
What We Are Building
A small page with a form. The user enters:
Ingredients
Packed-on date
Best-before date
Licence number (the 14-digit FSSAI number printed on Indian food packs)
Seller contact details
When they click Check label, the page lists every issue it found, or shows a clean pass.
Where the Rules Come From
Rules should come from something real, not guesswork. The checks below follow the label checklist in this guide to the best spices online, which covers ingredients, dates, licence number, net weight, seller details and policies. We will code the ones that can be validated from text and dates alone.
A note on scope: this tool only checks the format and logic of what you type. It cannot confirm that a licence number is real or that a pack is genuine. For that, you still need to check the seller and the product yourself.
Step 1: The HTML
html
<!DOCTYPE html>
Spice Label Checker
<form id="label-form">
<label>Ingredients
<textarea name="ingredients" rows="2"></textarea>
</label>
<label>Packed on
<input type="date" name="packedOn" />
</label>
<label>Best before
<input type="date" name="bestBefore" />
</label>
<label>Licence number (14 digits)
<input type="text" name="fssai" inputmode="numeric" />
</label>
<label>Seller contact
<input type="text" name="sellerContact" />
</label>
<button type="submit">Check label</button>
</form>
<section aria-live="polite">
<h2 id="status"></h2>
<ul id="issues"></ul>
</section>
Step 2: The Validation Function
Create app.js. The core is one pure function that takes the label data and returns a list of issues. Keeping it separate from the DOM makes it easy to test.
js
function parseDate(value) {
const date = new Date(value);
return Number.isNaN(date.getTime()) ? null : date;
}
function checkLabel(label, today = new Date()) {
const issues = [];
const ingredients = (label.ingredients || "").trim();
if (!ingredients) {
issues.push("Ingredients are missing.");
} else if (ingredients.toLowerCase() === "spices") {
issues.push("Ingredients are too vague. Each spice should be named.");
}
const packed = parseDate(label.packedOn);
const best = parseDate(label.bestBefore);
if (!packed) issues.push("Packed-on date is missing or invalid.");
if (!best) issues.push("Best-before date is missing or invalid.");
if (packed && best && best <= packed) {
issues.push("Best-before date must be after the packed-on date.");
}
if (best && best < today) {
issues.push("This pack is past its best-before date.");
}
const licence = (label.fssai || "").replace(/\s+/g, "");
if (!/^\d{14}$/.test(licence)) {
issues.push("Licence number should be exactly 14 digits.");
}
if (!(label.sellerContact || "").trim()) {
issues.push("Seller contact details are missing.");
}
return { ok: issues.length === 0, issues };
}
Step 3: Connect It to the Form
js
const form = document.getElementById("label-form");
const statusEl = document.getElementById("status");
const issuesEl = document.getElementById("issues");
form.addEventListener("submit", (event) => {
event.preventDefault();
const data = Object.fromEntries(new FormData(form));
const result = checkLabel(data);
issuesEl.innerHTML = "";
if (result.ok) {
statusEl.textContent = "No label issues found.";
return;
}
statusEl.textContent = ${result.issues.length} issue(s) found;
result.issues.forEach((text) => {
const li = document.createElement("li");
li.textContent = text; // textContent avoids injecting raw HTML
issuesEl.appendChild(li);
});
});
Open index.html, leave the ingredients as just spices, and submit. You should see the vague-ingredients message along with any other missing fields.
Step 4: Test the Logic
Because checkLabel takes today as a parameter, you can test it with a fixed date instead of waiting for real time to pass.
js
const today = new Date("2026-11-25");
const good = {
ingredients: "Coriander, red chilli, cumin, turmeric",
packedOn: "2026-10-01",
bestBefore: "2027-04-01",
fssai: "12345678901234",
sellerContact: "support@example.com",
};
console.log(checkLabel(good, today).ok); // true
const bad = { ...good, ingredients: "spices", bestBefore: "2026-09-01" };
console.log(checkLabel(bad, today).issues);
// ["Ingredients are too vague...", "Best-before date must be after...", "This pack is past..."]
The licence number above is a made-up example, used only to test the format.
Step 5: Make It Better
Some easy upgrades to practise with:
Live feedback. Run the check on the input event and show issues as the user types.
Severity levels. Mark some issues as warnings and others as errors.
More fields. Add net weight and price per 100 g, then calculate and show it.
Save drafts. Use localStorage to keep the last entry.
Reuse the function. Because it is pure, you can drop it into a React or Vue component or a Node script.
Common Mistakes
Comparing date strings instead of Date objects.
Forgetting that new Date("invalid") returns an “Invalid Date” object, not null.
Using innerHTML with user input.
Putting validation logic inside the click handler, which makes it hard to test.
Treating a format check as proof that a product is genuine.
Where This Idea Is Useful
If you are building a food or ecommerce site, a label checklist like this can help buyers compare products, whether they want to buy Indian spices, snacks or anything else sold with a printed label. A seller-side version also helps catch missing fields before a product page goes live. For a list of what to look for in authentic Indian spices, and which basics most kitchens stock first, see this guide to the best Indian spices.
FAQ
Does this tell me if a spice is authentic?
No. It only checks whether the label information looks complete and consistent. Smell, texture and seller reputation still matter.
Why does checkLabel take today as an argument?
It makes the function predictable and easy to test, because the result no longer depends on the real clock.
Can I use it for other products?
Yes. Change the ingredient rule and the licence format, and it works for most packaged foods.
Why not use a library?
For a small tool like this, plain JavaScript is simpler and teaches you the DOM and Date basics.
Should I validate on the server too?
Yes, if you store or act on the data. Client-side checks are a convenience, not a security measure.
Wrapping Up
In under 100 lines, we built a label checker that validates text, dates and a number format, with a pure function that is easy to test. It is a small project, but it covers forms, FormData, dates, regular expressions and safe DOM updates.
If you try it, tell me which field you would add next.
Top comments (0)