QUICK START:HTMLCSSJavaScriptTypeScriptPythonSQLReactNext.jsNode.jsDSASystem DesignDevOpsCybersecurityAI / ML
Intermediate 18 min readModule: Module 10: Forms & Controlled vs Uncontrolled Inputs

Controlled vs Uncontrolled Forms & Validation

Manage form inputs with controlled state, handle complex forms, and validate inputs before submission.

What You Will Learn in This Lesson

  • Controlled inputs (value + onChange state sync)
  • Uncontrolled inputs with FormData and defaultValue
  • Form validation and preventing default submission reloads

Introduction & Core Concept

In React, form inputs can be Controlled (React state drives the input value) or Uncontrolled (DOM manages its own state and reads values on submit).
WHY DOES THIS MATTER IN THE REAL WORLD?

Controlled inputs enable instant character validation, masked formatting (e.g. credit card spacing), and dynamic submit button disabling.

Controlled Form Component

javascript
javascript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function ContactForm() {
const [email, setEmail] = React.useState("");
const handleSubmit = (e) => {
e.preventDefault();
console.log("Submitting:", email);
};
return (
<form onSubmit={handleSubmit}>
<input
value={email}
onChange={(e) => setEmail(e.target.value)}
placeholder="name@example.com"
/>
<button type="submit" disabled={!email.includes("@")}>Submit</button>
</form>
);
}

Line-by-Line Technical Breakdown

1e.preventDefault() stops the browser from triggering a full page refresh on submit.

Try It Yourself (Interactive Editor)

Modify the code in real-time and click Run to test live browser output and console logs.

Intelligent Code Runner & Live Sandbox[JAVASCRIPT]
JAVASCRIPT SOURCE EDITOR
Interactive Live Code

Industry Best Practices & Professional Standards

  • Use uncontrolled inputs with FormData for simple high-performance forms with many fields.

Lesson Summary & Core Takeaways

  • React provides full control over form validation, masking, and submission.