JavaScript Simplified · One concept, one day, one project · Day 20 of 32
Form Validation
Checking that what a user typed makes sense, and telling them clearly when it does not. You build a signup validator with real error messages.
By the end of today you can
- Check for empty fields before accepting a form
- Do a simple email check and a password length check
- Show clear error messages next to the problem
- Build a signup form validator
Users make mistakes: a blank field, a typo in an email, a password that is far too short. Validation is checking the input before you trust it, and telling the user kindly what to fix. It is what separates a toy form from a real one.
1. Checking for Empty Fields
The most common problem is a blank field. A value that is an empty string means nothing was typed.
trim() removes spaces from the ends, so a field with only spaces still counts as empty. A small check that catches a very common mistake.2. Email and Password Checks
A full email check is complex, but a simple, honest one asks: is there an @ and a dot? For a password, the simplest useful rule is a minimum length.
includes from Week 3 checks for an @ and a dot. .length checks the password is at least six characters. Simple, clear rules a beginner can trust.3. Showing Error Messages
A good form does not just refuse; it says what is wrong, next to the field. The pattern: check each field, collect any problems, and show them.
The password "123" is too short, so the checks fall through to the length rule and show its message. Fix the password and the same form would show success. The order of checks matters: empty first, then shape, then length.
Today’s Mini Project
🔐 Signup Form Validator
The page above is your project: a signup form that checks for empty fields, a sensible email and a long enough password, and shows a clear message for each problem, or success when all is well.
Add a "confirm password" field and an error when it does not match the password. Expected: mismatched passwords show "Passwords do not match."
Change the minimum password length to 8 and test with a 6-character password.
Expected: the 6-character password is now refused with the length message.
You have finished Layer 3’s final piece. Your pages now take input, check it, and respond with care. That is a complete conversation with the user. Next week you make your JavaScript more modern and expressive.
Checkpoint
Why use trim() when checking for an empty field?
Because a field with only spaces is really empty to a user. trim() removes the spaces at the ends, so " " is caught as empty.
What is a simple, honest email check for a beginner?
Check that the value includes an @ and a dot. It is not perfect, but it catches obvious mistakes and is easy to understand.
Why does the order of validation checks matter?
Because you show the first problem found. Checking empty first, then shape, then length gives the most sensible message, rather than complaining about length on a blank field.
Tick Before You Move On
- ☐ I can catch empty fields, including spaces, with trim
- ☐ I can do a simple email check and a password length check
- ☐ I can show a clear error message for each problem
- ☐ I built the Signup Form Validator
Quick Recap
Validation checks input before you trust it, and guides the user. trim catches blank and spaces-only fields. A simple email check looks for @ and a dot; a password check uses length. Show the first problem clearly, in a sensible order.
Next: Day 21, and a new layer: modern JavaScript. You start with scope, understanding where a variable can and cannot be reached.