Sub topic 4
Validation Rules and When to Run Them
This chapter assumes you know basic HTML form elements, JavaScript or TypeScript, and how a browser submits form data. The goal is to choose validation timing that helps users correct mistakes without interrupting them.
Validation checks whether a value meets a rule. A rule might require an email address, limit a name to 100 characters, or confirm that a password matches. Client-side validation gives immediate feedback, but the server must validate again because browser code can be changed or bypassed.
Choose the right validation moment
Validation timing affects both usability and error recovery. Running every rule on every keystroke can make a form feel noisy. Waiting until final submission can make errors harder to find.
| Moment | Useful for | Common risk |
|---|---|---|
| On input | Format hints, character limits, password strength | Errors appear before the user finishes |
| On blur | Required fields and simple format checks | Users may miss errors that appear far away |
| On step change | Complete validation for the current step | The user cannot continue until the step is valid |
| On submit | Final client and server checks | Many errors may appear at once |
A practical strategy is to combine these moments:
- Do not show a required-field error while a user is typing into an untouched field.
- After the user leaves a field, validate it on blur.
- After an invalid field has been touched, update its message on input when useful.
- Validate every field in the current step when the user selects Next.
- Validate the complete payload on the server when the user submits.
The touched state records whether the user has left or interacted with a field. The dirty state records whether its value differs from its initial value. These states let you separate “this value is invalid” from “this user is ready to see the error.”
Write rules as predictable functions
Keep validation rules independent from the user interface. A validator should receive data and return errors. An error map associates each field name with one actionable message.
type Values = {
email: string;
name: string;
};
type Errors = Partial<Record<keyof Values, string>>;
function validate(values: Values): Errors {
const errors: Errors = {};
const email = values.email.trim();
if (!email) {
errors.email = "Enter your email address.";
} else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
errors.email = "Enter an email address such as [email protected].";
}
if (!values.name.trim()) {
errors.name = "Enter your full name.";
}
return errors;
}
const errors = validate({
email: "sam@example",
name: "",
});
console.log(errors);
// { email: "Enter an email address such as [email protected].",
// name: "Enter your full name." }An actionable message says what went wrong and how to fix it. Avoid messages such as “Invalid input” or “Validation failed.” If a rule has a complicated format, show an example rather than forcing users to guess.
Validate steps without losing values
In a multi-step flow, Next should validate the current step before changing the step. Back should normally move freely and preserve entered values. A later step should not validate fields that the user has not reached yet.
When a step fails validation, keep the user on that step, associate each message with its field, and move focus to the first invalid field. Do not clear valid values when displaying errors.
Exercise
Exercise
A form shows “Email is required” as soon as the user focuses an empty email field. What timing change would make this less disruptive while still giving prompt feedback?
Show answer
Wait until the field is blurred, or until the user tries to continue. After the field has been touched, update its message as the user edits it.