BeginnerCoder

Lesson 24 — Form validation

Validate before submit

Good forms check input while the user types: show an error as soon as the value is wrong, and keep the submit button disabled until it is right. The check is a derived value — computed from state on every render, not stored separately.

const strong = pw.length >= 8 && /\d/.test(pw);
<button disabled={!strong}>Save</button>

Note: Next.js pages are React components, so this preview runs your code with plain React in the browser. Next.js-only features (the file router, server rendering, data fetching on the server) are not running here — where a lesson needs them, it shows a small simulation instead.

Why it matters: instant feedback stops bad data and saves users from a confusing failed submit.

Want it another way?
Task

Make ok require both "@" and "." in the email, so typing "a@b" still shows "Invalid email".

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.

Keep going with Next.js Foundations

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.