RegEx word-count validator for a contest entry
A lightweight regex-based word counter that enforced a 300-word limit on contest entries — including the edge cases that the off-the-shelf options missed.
Problem
A 77kids contest invited customers to write in about their child. With a large expected entry volume, the contest needed a strict word limit on entries — both to keep entries reviewable and to enforce fair constraints. The available off-the-shelf word-count snippets didn't handle the edge cases the contest team cared about: leading whitespace, browser-specific quirks, and giving users live feedback as they typed instead of failing at submit.
Approach
I wrote a small RegEx-based word counter that did three things well:
wordCounter()— read the textarea value, trimmed leading whitespace, and used a regex to split into a word array. The total accounted for user-agent quirks where naive splits over-count.liveCount()— updated the UI on every keypress, changing color when the user crossed the limit so they got immediate feedback instead of a submit-time error.wordLimit()— a pure check used at submit time to enforce the constraint, sharing logic with the live counter so the two could never disagree.
Outcome
- Reliable input validation for a high-volume contest
- Customers got real-time feedback while writing instead of getting bounced after composing
- A small enough utility that it slotted into other forms across the marketing site
What this says about how I work
Most of this case study's value is in what it isn't: an over-engineered solution. The win was understanding that the real bug in off-the-shelf word counters was the edge cases — and writing the smallest possible thing that handled them correctly. Tiny tools, used at the right moment, can prevent disproportionate amounts of customer-facing pain.
The demo enforces a 10-word limit (rather than 300) so you can see the live counter cross the threshold without writing a novel.