smartyfy.mrindigi.com

Web Development Pro | Lesson 008 | Validation and Understandable Errors

Web Development Pro · Lesson 08 of 60

Module 1: HTML foundations

In this lesson

Prerequisites and learning outcomes

Complete lesson 7 first. This lesson builds on native constraints and accessible instructions. By the end, you can design validation that helps users recover without treating it as security. Plan 10–15 minutes for the core explanation and additional time for the worked practice.

Validation should help a person recover

A workshop form accepts one to four practice seats and requires the learner to choose a session. Validation can catch missing or obviously unsuitable values before submission, but the goal is not to punish mistakes. Tell people what is expected before they enter data, identify the actual problem, and preserve the information they already provided.

Browser validation and server validation solve different parts of the problem. HTML constraints improve the experience in a cooperating browser. They can be bypassed by modifying markup or sending a request directly. A real receiving server must independently check every important rule. A successful browser validation check is never proof that a request is authorized or safe.

Start with native constraints

Required prevents an applicable empty control from passing validation. Min, max, and step constrain number inputs. A select can begin with an empty option so required demands a real choice. Use visible wording such as “required” rather than expecting a colored star to communicate the rule by itself.

<form action="preview.html" method="get">
  <p>Local practice only; no booking is made.</p>
  <label for="session">Session (required)</label>
  <select id="session" name="session" required>
    <option value="">Choose a session</option>
    <option value="bicycle">Bicycle basics</option>
    <option value="mending">Mending clothes</option>
  </select>
  <label for="seats">Practice seats (required)</label>
  <p id="seats-help">Enter a whole number from 1 to 4.</p>
  <input id="seats" name="seats" type="number"
         min="1" max="4" step="1" required
         aria-describedby="seats-help">
  <button type="submit">Check practice values</button>
</form>

Aria-describedby links the seats control to its extra explanation. The visible label remains its name. Separating name and description avoids turning the control's name into a long paragraph while still making the constraint available. The id in aria-describedby must exist, just like a fragment link target.

Know what each check can establish

For seats, an empty value fails required, zero is below min, five is above max, and 2.5 does not satisfy the whole-number step. The number three fits these constraints. That still does not mean three real seats are available: availability is a separate business rule that requires authoritative data and server-side coordination.

An email input checks a browser-defined address shape, not whether a mailbox exists or belongs to the person using the form. A pattern checks a format, not the truth of the information. Avoid restrictive patterns for names and other human information unless the real task requires them. People's names and addresses do not fit one culture's assumptions.

Boolean HTML attributes depend on their presence. Required="false" still makes the input required; remove the attribute to remove that constraint. This is a common source of confusion when generating HTML. Later JavaScript will use boolean properties directly, but the markup rule remains important when reviewing a saved document.

Design understandable error feedback

Native browser messages vary by browser and language. They are useful for this first implementation, but a complete product may need a visible summary and field-specific messages that fit the task. A useful message says “Enter a whole number from 1 to 4,” rather than “Invalid input.” Do not rely only on red borders, which can be missed by people who do not perceive that color difference.

When you later add custom errors, connect the message to its field, identify the invalid state, and move focus only when it helps the user find the problem. Avoid announcing the same warning on every keystroke. A person midway through typing should not face a constant stream of interruptions before they have had a fair chance to finish.

Client-side validation must not erase completed fields after a mistake. If the server rejects a real submission, preserve safe values and explain what happened without exposing private internals. A network failure is also different from a bad field value; do not tell someone their name is invalid when the service is temporarily unavailable.

Test the boundary, not just the happy path

Write a small matrix before testing: empty session, empty seats, zero, one, four, five, and a decimal. Predict the outcome for each. Then observe the actual browser when available. Static checks can confirm that min="1" and max="4" are present, but they do not execute the browser's constraint validation algorithm or reveal its focus behavior.

For this local course package, the preview page is intentionally a demonstration destination. It states that no reservation occurred and does not claim the entered values were accepted by a real service. Back up the example before removing constraints to explore their effects, and never use this form to collect real attendee information.

Worked practice

Worked exercise 1: Fix a false boolean

Task: A developer writes required="false" to make seats optional. Worked answer: remove required entirely. Its presence activates the boolean attribute regardless of the string value. Keep the visible label consistent by also removing “required” if the field is genuinely optional.

Worked exercise 2: Choose boundary cases

Task: Test a whole-number range from one through four. Worked answer: include 0 and 5 to test outside boundaries, 1 and 4 to test included boundaries, 2.5 for step mismatch, and empty input for required. Testing only 2 would miss several distinct rules.

Worked exercise 3: Improve an error

Task: Replace “Bad value” beside the seats field. Worked answer: use “Enter a whole number from 1 to 4” and associate the message with that field. This states both the accepted range and the whole-number requirement, giving the learner a specific correction.

Knowledge check

Question 1

What does required="false" do in HTML? A) Makes the field optional. B) Keeps it required. C) Requires it only after typing starts. D) Makes it readonly.

Check question 1: answer and explanation

Answer: B. Boolean attributes are true by presence, not by interpreting their string as a boolean.

Question 2

Which value satisfies min=1, max=4, step=1? A) 0. B) 2.5. C) 3. D) 5.

Check question 2: answer and explanation

Answer: C. Three is within the inclusive bounds and follows the whole-number step.

Question 3

What does aria-describedby reference? A) An element containing extra descriptive text. B) The input type that performs validation. C) The name used in submitted data. D) An external help-page URL.

Check question 3: answer and explanation

Answer: A. It connects a control to additional explanation in the document.

Question 4

Does browser validation replace server validation? A) Yes, if all controls use native types. B) Yes, if the form uses POST. C) Yes, if the page is served over HTTPS. D) No.

Check question 4: answer and explanation

Answer: D. Requests can bypass the browser interface, so the server must enforce its own rules.

Question 5

What does an email input's format check prove? A) Mailbox ownership. B) A browser-accepted address shape. C) Successful delivery. D) The person's identity.

Check question 5: answer and explanation

Answer: B. Syntax acceptance does not establish real-world validity or authorization.

Sources and further reading