HTML Forms & User Input Validation Quiz — Questions & Answers

This quiz contains 10 questions. Below is a complete reference of all questions, answer choices, and correct answers. You can use this section to review after taking the interactive quiz above.

  1. Question 1: Required Fields in HTML Forms

    How can you make a form input field mandatory using only basic HTML attributes?

    • Add the required attribute to the input field.
    • Set the type to 'mandatory'.
    • Use the 'mustfill' attribute.
    • Add required='yes' to the input.
    • Insert minlength='1'.
    Show correct answer

    Correct answer: Add the required attribute to the input field.

  2. Question 2: Number Input Validation

    Which HTML attribute restricts a number input to accept values within a specific range?

    • min and max
    • pattern
    • range
    • step
    • length
    Show correct answer

    Correct answer: min and max

  3. Question 3: Pattern Validation for Input

    To ensure an input only allows a 5-digit ZIP code, which attribute and value would you use?

    • pattern='\d{5}'
    • maxlength='5'
    • required='5digits'
    • accept='5'
    • data-validate='zip'
    Show correct answer

    Correct answer: pattern='\d{5}'

  4. Question 4: Client-Side vs Server-Side Validation

    Why is server-side validation necessary, even if client-side validation is present?

    • Client-side validation can be bypassed by users.
    • Server-side validation is always faster.
    • HTML cannot do any validation by itself.
    • Browsers disable client-side checks by default.
    • It is preferred for accessibility reasons only.
    Show correct answer

    Correct answer: Client-side validation can be bypassed by users.

  5. Question 5: Using Input Type 'number'

    What is a common issue when using <input type='number'> for value retrieval in JavaScript?

    • Invalid values result in an empty string from .value.
    • Numbers are always returned as strings.
    • The input cannot be left empty.
    • Only integers are allowed by default.
    • It does not support min or max attributes.
    Show correct answer

    Correct answer: Invalid values result in an empty string from .value.

  6. Question 6: Limiting Input to Integers Only

    How would you enforce that an input accepts only integer values using HTML attributes?

    • Set step='1' on an input with type='number'.
    • Use pattern='[0-9]*' on a text input.
    • Add onlyint='true'.
    • Use accept='integer'.
    • Set integer='yes'.
    Show correct answer

    Correct answer: Set step='1' on an input with type='number'.

  7. Question 7: The Purpose of the 'pattern' Attribute

    What does the pattern attribute enforce on an <input> field?

    • A regular expression constraint on the value.
    • A maximum value limit.
    • A format for placeholder text.
    • Preset valid options.
    • The type of keyboard to display.
    Show correct answer

    Correct answer: A regular expression constraint on the value.

  8. Question 8: Preventing XSS Attacks in Forms

    Which practice helps prevent XSS attacks when handling user-submitted form data?

    • Sanitize and validate all user input before processing.
    • Allow all inputs for flexibility.
    • Use only HTTP GET for form submission.
    • Disable SSL on the form.
    • Trust data from authenticated users.
    Show correct answer

    Correct answer: Sanitize and validate all user input before processing.

  9. Question 9: Best Attribute for Email Validation

    Which HTML input type provides basic built-in email format validation?

    • type='email'
    • pattern='@'
    • type='mail'
    • inputmode='email'
    • email='true'
    Show correct answer

    Correct answer: type='email'

  10. Question 10: Accessibility and Form Validation

    Which technique improves accessibility for all users when validating form inputs?

    • Provide clear labels and error messages for each input.
    • Set hidden fields to trigger errors.
    • Use JavaScript-only validation with no summaries.
    • Only display errors after multiple submissions.
    • Hide error messages from screen readers.
    Show correct answer

    Correct answer: Provide clear labels and error messages for each input.