Module 35: Forms and Validation in React

Lesson 2 of 5 · 15–25 minutes

Validation rules

🏠The everyday picture

A checklist the clerk runs through before accepting a form: name present, email looks right, message long enough. The same checklist every time, written down once.

📘What it really is

type Errors = Partial<Record<keyof EnquiryForm, string>>;

function validate(form: EnquiryForm): Errors {
  const errors: Errors = {};
  if (!form.name.trim()) errors.name = "Please enter your name.";
  if (!/^\S+@\S+\.\S+$/.test(form.email)) errors.email = "Please enter an email like name@example.com.";
  if (form.message.trim().length < 10) errors.message = "Please write at least 10 characters.";
  return errors;
}

🖱️Try it

Write validate in src/validation.ts. Add rules: subject between 3 and 100 characters; message at most 2,000 characters. Write at least six tests for it, including spaces-only input and a long message.

✅Quick check

Why write validation as a separate pure function?

Show the answer

It can be tested on its own, reused, and keeps the component simpler.

Your React form validates perfectly. Does the server still need to check?

Show the answer

Yes. Requests can be sent without your form; the server must always validate.

← PreviousForm state
ProLearners Assist