HTML · Form Validation & Advanced Forms · Lesson 12 of 32

Form Validation & Advanced Forms

Form Validation & Advanced Forms

Form validation is the process of checking whether the information entered by a user is complete and follows the expected format before it is processed.

HTML provides several built-in validation features that can perform basic validation without JavaScript.

Key Concept:

HTML validation improves the quality of user input, but it should not be considered a replacement for server-side validation.

1. Required Fields

The required attribute specifies that a form control must contain a value before the form can normally be submitted.

<form>

    <label for="name">Name:</label>

    <input type="text"
           id="name"
           name="name"
           required>

    <button type="submit">Submit</button>

</form>

If the user attempts to submit the form without entering a value, the browser displays a validation message.

Exam Tip:

required is a Boolean attribute. Its presence is sufficient; it does not require a value such as required="true".

2. min and max

The min and max attributes define the permitted numerical range for suitable input types.

<label for="age">Age:</label>

<input type="number"
       id="age"
       name="age"
       min="5"
       max="100"
       required>

Values below 5 or above 100 will fail the browser's constraint validation.

3. minlength and maxlength

These attributes control the number of characters that can be entered into suitable text-based controls.

<label for="username">Username:</label>

<input type="text"
       id="username"
       name="username"
       minlength="5"
       maxlength="20"
       required>
Attribute Purpose
minlength Defines the minimum number of characters.
maxlength Defines the maximum number of characters.

4. The pattern Attribute

The pattern attribute specifies a regular expression that the entered value must match.

Example

<label for="code">Student Code:</label>

<input type="text"
       id="code"
       name="code"
       pattern="[A-Z]{2}[0-9]{4}"
       required>

The pattern above expects two uppercase letters followed by four digits.

Examples:

  • AB1234 → Valid pattern
  • A12345 → Does not match the pattern
  • ab1234 → Does not match the pattern
Important:

A pattern is a validation rule, not a replacement for server-side validation.

5. Email Validation

The email input type provides built-in browser validation appropriate for email address input.

<label for="email">Email:</label>

<input type="email"
       id="email"
       name="email"
       required>

The browser checks whether the entered value conforms to an email-address format.

Best Practice:

Use type="email" instead of type="text" when the expected data is an email address.

6. URL Validation

The url input type is designed for web addresses and provides browser-level validation.

<label for="website">Website:</label>

<input type="url"
       id="website"
       name="website"
       placeholder="https://example.com">

7. The step Attribute

The step attribute specifies the permitted intervals for suitable numerical and date/time controls.

<label for="quantity">Quantity:</label>

<input type="number"
       id="quantity"
       name="quantity"
       min="0"
       max="100"
       step="5">

With step="5", values can progress in increments of five within the permitted range.

8. The textarea Element

The <textarea> element is used for entering multiple lines of text.

<label for="message">Message:</label>

<textarea id="message"
          name="message"
          rows="5"
          cols="40"></textarea>

Validation Example

<textarea id="message"
          name="message"
          rows="5"
          minlength="20"
          maxlength="500"
          required></textarea>
Attribute Purpose
rows Specifies the visible number of text rows.
cols Specifies the visible width in character columns.
maxlength Limits the maximum number of characters.
minlength Specifies the minimum number of characters.

9. The select Element

The <select> element creates a drop-down list from which the user can select an option.

<label for="course">Choose a Course:</label>

<select id="course" name="course" required>

    <option value="">-- Select Course --</option>
    <option value="html">HTML</option>
    <option value="css">CSS</option>
    <option value="javascript">JavaScript</option>

</select>
Important:

The value attribute of an <option> is the value submitted with the form.

10. Multiple Selection

Adding the multiple attribute allows the user to select more than one option.

<label for="skills">Select Skills:</label>

<select id="skills"
        name="skills"
        multiple>

    <option value="html">HTML</option>
    <option value="css">CSS</option>
    <option value="javascript">JavaScript</option>
    <option value="python">Python</option>

</select>

This is useful when the user needs to select multiple items from a predefined list.

11. Grouping Options with optgroup

The <optgroup> element groups related options inside a drop-down list.

<label for="course">Select Course:</label>

<select id="course" name="course">

    <optgroup label="Web Development">
        <option value="html">HTML</option>
        <option value="css">CSS</option>
        <option value="javascript">JavaScript</option>
    </optgroup>

    <optgroup label="Programming">
        <option value="python">Python</option>
        <option value="java">Java</option>
    </optgroup>

</select>

12. The datalist Element

The <datalist> element provides a list of suggested values for an input field.

<label for="browser">Choose a Browser:</label>

<input list="browsers"
       id="browser"
       name="browser">

<datalist id="browsers">

    <option value="Google Chrome">
    <option value="Mozilla Firefox">
    <option value="Microsoft Edge">
    <option value="Safari">

</datalist>
Element Purpose
<select> User selects from predefined options.
<datalist> Provides suggestions while allowing input.

13. Grouping Controls with fieldset

The <fieldset> element groups related form controls.

<fieldset>

    <legend>Personal Information</legend>

    <label for="name">Name:</label>
    <input type="text"
           id="name"
           name="name">

    <label for="email">Email:</label>
    <input type="email"
           id="email"
           name="email">

</fieldset>

Grouping related controls improves the structure and accessibility of complex forms.

14. The legend Element

The <legend> element provides a caption or title for a <fieldset>.

<fieldset>

    <legend>Contact Information</legend>

    <label for="phone">Phone:</label>
    <input type="tel"
           id="phone"
           name="phone">

</fieldset>
Remember:

fieldset → Groups related controls

legend → Gives the group a caption

15. disabled Controls

The disabled attribute prevents a form control from being interacted with.

<label for="country">Country:</label>

<input type="text"
       id="country"
       name="country"
       value="Canada"
       disabled>
Exam Tip:

A disabled form control generally does not participate in form submission.

16. readonly Controls

The readonly attribute prevents the user from editing the value while keeping the control available for normal interaction such as focusing.

<label for="student-id">Student ID:</label>

<input type="text"
       id="student-id"
       name="student-id"
       value="STU1025"
       readonly>
Feature readonly disabled
User can edit No No
Normally submitted Yes No
Control available Yes No

17. autocomplete

The autocomplete attribute provides hints to the browser about whether it may use previously entered information to assist the user.

<label for="email">Email:</label>

<input type="email"
       id="email"
       name="email"
       autocomplete="email">

Common autocomplete tokens include values such as name, email, tel and street-address.

18. The novalidate Attribute

The novalidate attribute on a <form> tells the browser not to perform its normal built-in constraint validation when submitting that form.

<form novalidate>

    <label for="email">Email:</label>

    <input type="email"
           id="email"
           name="email">

    <button type="submit">Submit</button>

</form>
Important:

novalidate does not mean that validation is unnecessary. It simply disables the browser's normal built-in validation for that form submission.

19. Important HTML Validation Attributes

Attribute Purpose Typical Use
required Requires a value. Mandatory fields
min Minimum permitted value. Age, quantity
max Maximum permitted value. Age, marks
minlength Minimum number of characters. Username, message
maxlength Maximum number of characters. Message, description
pattern Requires a matching pattern. Codes, structured input
step Defines permitted increments. Number and range inputs
multiple Allows multiple values/selections where supported. File and select controls

20. Complete Advanced Form Example

The following example demonstrates several HTML form features together.

<form>

    <fieldset>

        <legend>Personal Information</legend>

        <label for="name">Full Name:</label>

        <input type="text"
               id="name"
               name="name"
               minlength="3"
               maxlength="50"
               required>

        <br><br>

        <label for="email">Email:</label>

        <input type="email"
               id="email"
               name="email"
               autocomplete="email"
               required>

        <br><br>

        <label for="age">Age:</label>

        <input type="number"
               id="age"
               name="age"
               min="5"
               max="100"
               required>

    </fieldset>


    <fieldset>

        <legend>Course Information</legend>

        <label for="course">Course:</label>

        <select id="course"
                name="course"
                required>

            <option value="">-- Select Course --</option>
            <option value="html">HTML</option>
            <option value="css">CSS</option>
            <option value="javascript">JavaScript</option>

        </select>

        <br><br>

        <label for="message">Learning Goal:</label>

        <textarea id="message"
                  name="message"
                  rows="5"
                  minlength="20"
                  maxlength="300"
                  required></textarea>

    </fieldset>


    <button type="submit">Register</button>

    <button type="reset">Clear</button>

</form>

21. Client-Side vs Server-Side Validation

Feature Client-Side Validation Server-Side Validation
Where it occurs Browser Server
Purpose Immediate feedback Reliable data validation
Can user bypass it? Yes Should not be trusted to the client
Examples required, pattern, min, max Application/database validation
Professional Rule:

Validate input on the client for usability and again on the server for reliability and security.

22. Accessible Advanced Forms

A professional form should be usable by people with different abilities and assistive technologies.

Best Practices

  • Associate form controls with meaningful <label> elements.
  • Use fieldset and legend to group related controls.
  • Provide clear instructions for complex fields.
  • Do not rely only on colour to communicate errors.
  • Use appropriate input types instead of generic text fields whenever possible.
  • Ensure the form can be operated using the keyboard.

23. Common Mistakes

  • Using placeholder instead of a proper label.
  • Assuming browser validation provides complete security.
  • Forgetting the name attribute.
  • Using disabled when the value actually needs to be submitted.
  • Using a text box where select, radio or checkbox would be more appropriate.
  • Creating complex forms without grouping related controls using fieldset.
  • Using a pattern without understanding the regular expression.
  • Trusting client-side validation alone.

24. Exam Questions

Question 1: What is form validation?

Click to View Answer

Form validation is the process of checking whether user-entered data is complete and conforms to specified requirements before it is processed.

Question 2: Which HTML attribute makes a form field mandatory?

Click to View Answer

required.

Question 3: What is the purpose of the pattern attribute?

Click to View Answer

It specifies a pattern that the entered value must match for the control to pass constraint validation.

Question 4: Differentiate between minlength and maxlength.

Click to View Answer

minlength specifies the minimum number of characters, while maxlength specifies the maximum number of characters.

Question 5: What is the purpose of the fieldset element?

Click to View Answer

It groups related form controls into a logical section.

Question 6: What is the difference between select and datalist?

Click to View Answer

A select provides a predefined set of options from which the user selects, while a datalist provides suggestions for an input while still allowing the user to enter a value.

Question 7: What is the difference between readonly and disabled?

Click to View Answer

A readonly control cannot normally be edited but remains available for form submission, whereas a disabled control is unavailable and is generally excluded from form submission.

Question 8: Can HTML validation alone secure an application?

Click to View Answer

No. HTML validation is client-side validation. Applications must also validate and sanitize data on the server.

25. Interview Questions

1. Why is server-side validation necessary if HTML already validates the form?

Click to View Answer

Client-side validation can be bypassed or manipulated. Server-side validation ensures that data is checked before it is trusted or stored by the application.

2. What is the difference between required and pattern?

Click to View Answer

required checks that a value has been supplied, whereas pattern checks whether the supplied value follows a specified pattern.

3. Why would you use fieldset and legend?

Click to View Answer

They provide logical grouping and a caption for related controls, improving form structure and accessibility.

4. What is the difference between textarea and input type="text"?

Click to View Answer

input type="text" is normally used for a single line of text, while textarea is designed for multi-line text.

5. When would you use datalist instead of select?

Click to View Answer

Use datalist when you want to offer suggested values while still allowing the user to enter a value. Use select when the user should choose from a defined set of options.

26. Practical Challenge

Create an accessible course-registration form with the following requirements:

  • Full name: required, minimum 3 characters.
  • Email: required and use the email input type.
  • Password: required and minimum 8 characters.
  • Age: between 10 and 100.
  • Course: select from a drop-down list.
  • Skills: allow multiple selections.
  • Learning goals: multi-line textarea.
  • Student code: two uppercase letters followed by four digits.
  • Group related fields using fieldset and legend.
  • Provide Submit and Reset controls.
Advanced Challenge:

Add appropriate autocomplete values, meaningful labels and browser validation without using JavaScript.

Quick Revision

  • required → Field must contain a value.
  • min / max → Define numerical limits.
  • minlength / maxlength → Define character limits.
  • pattern → Matches a specified pattern.
  • step → Defines permitted increments.
  • textarea → Multi-line text.
  • select → Drop-down selection.
  • multiple → Allows multiple selections where supported.
  • optgroup → Groups related options.
  • datalist → Provides input suggestions.
  • fieldset → Groups related form controls.
  • legend → Provides a caption for a fieldset.
  • readonly → User cannot edit the value.
  • disabled → Control is unavailable and generally not submitted.
  • novalidate → Disables normal browser constraint validation for the form.
  • HTML validation improves usability, but server-side validation remains essential.
Memory Trick: Advanced Form Toolkit

Validate → required, min, max, pattern

Write → textarea

Select → select, option, optgroup

Suggest → datalist

Group → fieldset + legend

Control → readonly + disabled