Forms & Validation for AI Coders
Forms collect user input. Validation ensures the data is correct before submitting. Building a login form? See our authentication guide.
In this chapter
- How to add a form to your site with one AI prompt
- What form validation is and why it matters
- Where form data goes after the user clicks submit
- How to handle errors, required fields, and confirmations
Form Element Vocabulary
Validation Types
Client-Side Validation
Happens in the browser before submitting. Gives instant feedback.
- Required fields
- Email format check
- Min/max length
- Pattern matching
- Password strength
- Matching fields (confirm password)
Server-Side Validation
Happens on the server after submitting. The last line of defense. Data gets saved to your database.
- Always validate on server too
- Check data types and ranges
- Clean inputs to prevent attacks
- Check uniqueness (email taken?)
- Rate limiting
- Authentication checks
Popular Form Libraries
Common Form Patterns
Quick Alternative: No-Code Forms
Not ready to build a custom form? Use a hosted form tool and embed it on your site.
Real World Example: Recipe Submission Form
Imagine you're building a recipe sharing app. Users need a form to submit their recipes with a title, description, ingredients list, step-by-step instructions, cooking time, difficulty level, and a photo. The form needs validation (title required, at least 2 ingredients), a loading state while saving, and a success message.
“Build a recipe submission form with these fields: title (required, max 100 chars), description (textarea), ingredients (dynamic list: user can add/remove items, minimum 2), instructions (numbered steps, add/remove), cooking time (number in minutes), difficulty (select: Easy, Medium, Hard), and a photo upload with preview. Use React Hook Form with Zod validation. Show inline errors, disable submit while saving, and show a success toast when done. Style with Tailwind CSS.”
Prompt This
Copy-paste these prompts into your AI tool to apply what you just learned.
“Build a [contact/signup/feedback] form with fields for [name, email, message]. Include validation and show success/error messages after submission.”
“Add form validation to my existing form. Required fields should show red borders and error text. Email must be valid format. Password must be at least 8 characters.”
“Turn my form into a multi-step wizard with [2/3/4] steps. Add a progress bar at the top, Back and Next buttons, and validate each step before moving forward.”
Form UX Best Practices
Frequently Asked Questions
How do I validate form inputs?
Tell your AI tool what rules each field needs (required, email format, minimum length, etc.). It will add both client-side validation (instant feedback as users type) and server-side validation (checking again on the backend for security). Never trust client-side validation alone.
How do I handle form submissions?
For simple forms (contact, feedback), services like Formspree or Resend can receive submissions without a backend. For forms that save data to your app (signup, create post), you'll need an API route that processes the data and stores it in your database.
Why does my form data disappear when I refresh the page?
Form data lives in the browser's memory and resets on refresh. If you want data to persist, you need to save it: either to a database (for permanent storage) or localStorage (for temporary, browser-only storage). Tell your AI tool which approach you need.