Form Validation
Validate on submit or as the user types. Keep errors in state and show them near the field. Always validate on the server too.
Example
function validate(v) {
const e = {};
if (!v.email.includes("@")) e.email = "Invalid email";
if (v.pw.length < 8) e.pw = "Too short";
return e;
}
console.log(validate({ email: "a", pw: "123" }));
console.log(validate({ email: "a@b.c", pw: "12345678" }));Result
{ email: 'Invalid email', pw: 'Too short' }
{}Exercise
Check the length.
v.pw. < 8
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.