Forms and Input Validation
Getting data from humans safely - and never trusting a single character of it.
Before this: Components and User Interfaces
Objective
Direct an agent to build forms that collect input, validate it against explicit rules, show helpful errors, and treat every input as untrusted - your first real security habit.
Vocabulary
The words to use when directing your agent.
- form
- The UI pattern for collecting input: fields, labels, a submit action.
- validation
- Checking submitted values against rules before accepting them.
- event
- Something the user does that code reacts to - typing, submitting, clicking.
- error state
- The designed way your UI behaves when input breaks the rules.
- sanitizing
- Cleaning or rejecting input that could be dangerous, not just wrong.
Understand it
Rules as a contract
Write validation rules as a visible list: username 3-20 characters, letters and numbers only; email matches a basic pattern; password at least 8 characters. The list IS the specification - your agent codes the list, your tests check the list, your reviewers audit the list.
Errors help, they do not scold
'Invalid input' helps nobody. 'Password must be at least 8 characters' lets the user succeed. Every error message should state the rule that was broken.
Core concepts
Users will type anything
Empty submissions, 500-character names, emails without @, scripts pasted into fields. Assume every field will receive the worst possible value and design rules for it upfront.
Validation is a security habit
Checking input is your first line of defense, long before authentication enters the picture. The discipline you build here - explicit rules, reject early, explain failures - scales directly to server-side security later.
Examples
The three-layer check
Required field empty -> block submit, highlight the field. Email malformed -> message under the field. All valid -> success state. Walking the happy path AND each failure path is how you verify a form.
Dangerous input example
A comment box that renders whatever users type can execute scripts attackers paste into it. Rejecting or escaping such input is sanitizing - a preview of XSS, which you will meet properly in Level 2.
Common mistakes
Common mistake
Only testing the happy path.
Fix
Test every rule with a violating input, plus empty-everything submit. Your DoD should list them.
Common mistake
Vague error messages like 'Error!' or nothing at all.
Fix
Every broken rule maps to one specific, human message naming the rule.
Working with your AI agent
Give your agent the rules table FIRST, then ask it to enumerate the failure cases it will implement BEFORE coding. You review the case list - that is faster than reviewing code hunting for missed cases.
Build it
The module’s real project. Your agent builds; you direct and inspect.
Project: Signup Form With Rules
Build a signup form whose entire personality is its validation: a visible rules table, per-field error messages, and failure paths you can demo on demand.
Requirements
- Fields: username, email, password with labels
- Visible rules: username 3-20 chars alphanumeric; valid email pattern; password minimum 8 characters
- Submitting invalid input blocks and shows a specific message under each offending field
- Valid submission shows a designed success state (no real accounts being created)
Context for your agent
Build a client-side signup form. Implement exactly this validation table: username 3-20 alphanumeric characters; email basic pattern x@y.z; password at least 8 characters. Show a specific error message beneath each failing field on submit; block submission while any rule fails; show a success panel when all pass. Before coding, print the list of failure cases you will cover so I can approve it.
Verify it - twice
Check every item yourself first. Then hand your agent the prompt and compare verdicts.
Verification prompt - paste into your AI agent
Security-review my Signup Form. Verify PASS or FAIL with evidence: (1) all three rules implemented exactly as specified, (2) each failing field shows ITS OWN rule-naming message, (3) invalid submits are fully blocked, (4) no user input is rendered as raw HTML anywhere, (5) success state reachable only with all rules passing. Then attack the form yourself: try five abusive inputs (script tags, 10000-char strings, weird unicode) and report how the form behaved on each.
Run this against “Forms and Input Validation” after your own checklist pass. Where the agent and you disagree, investigate before moving on.
Review questions
Answer out loud. If you cannot, revisit before moving on.
Recite your form's rules table from memory.
Why is 'invalid input' a bad error message?
How does form validation preview real security thinking?
You can now
- Specify input rules as reviewable contracts
- Verify forms by walking failure paths, not just the happy path
- Apply never-trust-input as a standing security habit
Up next
APIs and JSON