Transactions
18 min read
Validate Dates, Descriptions, and Amounts
Do not let the form send obviously invalid values. Put the reusable rules in client/src/lib/finance.js.
The final project uses real calendar validation and a money parser that accepts at most two decimal places.
export function isValidDateOnly(value) {
if (typeof value !== "string" || !/^\d{4}-\d{2}-\d{2}$/.test(value)) return false;
const [year, month, day] = value.split("-").map(Number);
const date = new Date(Date.UTC(year, month - 1, day));
return (
date.getUTCFullYear() === year &&
date.getUTCMonth() === month - 1 &&
date.getUTCDate() === day
);
}
Test it
Try an empty description, a zero amount, 84.567, and an invalid calendar date such as February 30. Each should produce a clear validation message.
Checkpoint
The form rejects malformed input before making the API request.