Forms & Validation for AI Coders | SheCodes
Intermediate 6 min read

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

Text Input Names, titles, short text
Full Name
John Doe
Email Input Email with built-in validation
Email Address
Password Input Hidden text entry for passwords
Password
••••••••
Number Input Numeric values with up/down arrows
Quantity
42▲▼
Date Picker Calendar date selection
Date
2025-03-15
Textarea Multi-line text (messages, bios)
Message
Tell us about yourself...
This is a longer text area
for multi-line content.
Select / Dropdown Choose one from a list
Country
United States
Checkbox Toggle on/off, accept terms
Accept terms
Subscribe to newsletter
Radio Buttons Choose exactly one option
Small
Medium
Large
File Upload Upload images, documents
Attachment
Choose fileNo file chosen
Range Slider Select a value on a scale
Volume65%
Color Picker Select a color visually
Brand Color
#7c3aed

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
React Hook Form Lightweight, performant form library for React. The most popular choice. Recommended Zod Define what your data should look like and validate it in one place. Very popular. Validation
Formik Full-featured form library. More batteries-included than React Hook Form. Alternative
Yup Schema validation library. Often paired with Formik. Similar to Zod. Validation
React Final Form Fast form management that only updates what changed. Great for performance. Performance
Conform Form library that works even without JavaScript. Built for Next.js. Next.js

Common Form Patterns

Login Form Email Password Remember Me Forgot Password link Submit button Sign Up link
Registration Form Name Email Password Confirm Password Terms checkbox Submit button
Contact Form Name Email Subject (dropdown) Message (textarea) Submit button
Checkout Form Shipping address City State Zip Card number Expiry CVV Pay button
Profile Settings Avatar upload Display name Bio (textarea) Email Save button
Multi-Step Wizard Step indicator Back/Next buttons Form fields per step Summary step Submit

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 this with AI

“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 FORM

“Build a [contact/signup/feedback] form with fields for [name, email, message]. Include validation and show success/error messages after submission.”

ADD FORM VALIDATION

“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.”

MULTI-STEP FORM WIZARD

“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

Show errors inline Display error messages next to the field, not in a banner at the top.
Validate when user leaves a field Check fields when the user clicks or tabs away, not while they're still typing.
Disable submit during loading Show a spinner on the button and disable it to prevent double-submit.
Use placeholder wisely Placeholders disappear when typing. Always use a visible label too.
Auto-focus the first field Save users a click by placing the cursor in the first field when the form loads.
Show password toggle Let users toggle password visibility with an eye icon.

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.