AI Hints
Home›React Tutorial›Submitting and Validating
React · Lesson 30 of 120

Practice: A Signup Form

Forms together.

⏱ 2 min read▶ Video at 22:50🎓 Free
Watch this lesson in the React courseWatch on YouTube ↗Full React course

Let's practise forms. A signup form, with a name, an email, and a terms checkbox. Each field gets its own error, and the form only succeeds when everything is valid.

  • Name, email, terms
  • Errors per field
  • Success when valid

A complete signup

Form holds every field. Check builds an errors object, one entry per problem. On submit, if there are no errors, we show a welcome. We fill in a name, but forget the email and the terms. Run it.

App.jsxjsx
import { useState } from "react";
import SignupFields from "./SignupFields";
function check(f) {
  const errors = {};
  if (!f.name.trim()) errors.name = "Name is required";
  if (!f.email.includes("@")) errors.email = "Enter a valid email";
  if (!f.terms) errors.terms = "Please accept the terms";
  return errors;
}
export default function App() {
  const [f, setF] = useState({ name: "", email: "", terms: false });
  const [errors, setErrors] = useState({});
  const [ok, setOk] = useState(false);
  const set = e => setF({ ...f, [e.target.name]: e.target.type === "checkbox" ? e.target.checked : e.target.value });
  const submit = e => { e.preventDefault(); const er = check(f); setErrors(er); setOk(Object.keys(er).length === 0); };

  return ok ? <h2>Welcome, {f.name}!</h2> : (
    <form onSubmit={submit}><SignupFields f={f} set={set} errors={errors} /><button id="go">Sign up</button></form>);
}
Also uses style.css
body{font-family:-apple-system,Helvetica,Arial,sans-serif;margin:22px;font-size:21px;color:#1d1d1f;background:#fafafa}
h1{font-size:34px;margin:0 0 14px} h2{font-size:26px;margin:0 0 10px}
button{font-size:19px;padding:9px 18px;border-radius:9px;border:0;background:#61dafb;color:#111;font-weight:600;margin:4px 6px 4px 0}
input{font-size:19px;padding:8px 10px;border:2px solid #bbb;border-radius:8px}
li{margin:5px 0} .box{padding:14px;border-radius:12px;background:#fff;border:2px solid #ddd;margin:8px 0}
.done{text-decoration:line-through;color:#999} small{color:crimson;display:block;font-size:16px} .big{font-size:64px;font-weight:800}
Also uses SignupFields.jsx
export default function SignupFields({ f, set, errors }) {
  return (
    <>
      <input id="name" name="name" value={f.name} onChange={set} placeholder="Name" />{errors.name && <small>{errors.name}</small>}
      <input name="email" value={f.email} onChange={set} placeholder="Email" />{errors.email && <small>{errors.email}</small>}
      <label><input name="terms" type="checkbox" checked={f.terms} onChange={set} /> I accept the terms</label>
      {errors.terms && <small>{errors.terms}</small>}
    </>
  );
}
Also uses actions.json
[{"type": "#name", "text": "Leo"}, {"click": "#go"}]

Summary

An errors object, one entry per field, makes multi field validation simple. Next, lifting state up.

Key takeaways
  • errors object
  • One per field
  • Success when empty
Next up: Lifting State Up — share between siblings