r/Frontend • u/Less-Marsupial-7960 • 5h ago
When should a frontend form validate: while typing, on blur, or only on submit?
I've been thinking about how to handle validation in forms, especially when some fields require an API request.
For example, imagine a registration form with a username field. We need to check:
- Is the username in the correct format?
- Is it already taken?
- Is the availability request still relevant if the user keeps typing?
- What happens if an older API response arrives after a newer one?
There are a few approaches:
- Validate on every keystroke.
- Validate when the user leaves the field (blur).
- Debounce API checks while typing.
- Perform final validation again when the form is submitted.
Personally, I think a combination of immediate local validation, debounced availability checks and final server-side validation makes sense.
But I'm curious how other frontend developers handle this in production.
A few questions:
- Do you prefer validation on blur or while typing?
- How do you prevent stale API responses from showing incorrect errors?
- Do you use a form library or handle this logic yourself?
Interested in hearing what has worked well in real projects.