A real registration form checks several fields at once. This topic joins the earlier ideas into one complete check.
Checking Each Field
Each field has its own rule. A name is required and needs at least three characters. An email must match an email pattern. A password needs at least eight characters, with at least one letter and one digit. A confirm-password field must equal the password exactly. Table 4.4 lists the rules of the form built in this topic.
Table 4.4: Rules for a registration form
| Field | Rule | Message |
|---|---|---|
| Name | At least 3 characters | Name needs 3 or more letters |
| Matches email pattern | Enter a valid email | |
| Password | 8 or more, letter and digit | Password is too weak |
| Confirm | Equals password | Passwords do not match |
Definition: Password strength. A measure of how hard a password is to guess, judged by its length and variety of characters.
Collecting Errors
Instead of stopping at the first problem, a good form collects every error and shows them together, so that the user fixes everything in one pass. A common design is to keep an array of messages. Each rule pushes a message into the array when it fails. At the end, if the array is not empty, the handler calls preventDefault() and shows all the messages.
Giving Feedback
Messages should appear beside the fields they describe. A CSS class can mark a valid field in green and an invalid one in red. Old messages must be cleared at the start of each check, or they pile up. The password itself should never be printed in the page or the console.
Example 4.4: A registration check
function validate(name, email, pass, confirm) {
const errors = [];
if (name.trim().length < 3) {
errors.push("Name needs 3 or more letters");
}
const em = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
const pw = /^(?=.*[A-Za-z])(?=.*\d).{8,}$/;
if (!em.test(email)) errors.push("Enter a valid email");
if (!pw.test(pass)) errors.push("Password is too weak");
if (pass !== confirm) errors.push("Passwords do not match");
return errors;
}
console.log(validate("Al", "bad", "short", "other"));
console.log(validate("Asha", "a@b.com", "abc12345", "abc12345"));
Output:
[
'Name needs 3 or more letters',
'Enter a valid email',
'Password is too weak',
'Passwords do not match'
]
[]
The first call returns four messages, and the second returns an empty array, which means the form may be sent.
Common mistakes
- Stopping at the first error, which makes the user submit again and again.
- Forgetting to clear old messages before checking again.
- Comparing the two passwords with a loose check, or printing a password in the console.
- Using a different rule on the server from the one used on the client, which confuses users.
Key takeaways
- Give each field its own rule and its own message.
- Collect all errors in an array, and call preventDefault once at the end if any exist.
- Show messages beside fields and clear them before each check.
- Compare confirm password with the password using !==.
Practice questions
- Why collect all errors instead of stopping at the first?
- Which comparison tests that two passwords differ?
- When is preventDefault called in the registration check?
Answers to the practice questions
- The user can correct every problem in one pass.
- pass !== confirm.
- Once at the end, if the errors array is not empty.
Sources
- Official BCA syllabus (NEP 2025 scheme), Web Application Development, course code 25BCA405DS04, Unit IV
