HTML Form Validation

HTML Form Validation and Attributes: Guide the Right Input

Good forms don’t just ask for information — they help ensure that users give the right kind. With HTML5, many powerful validation rules are built in. You don’t need JavaScript to make sure users enter a valid email, set a minimum quantity, or fill out all required fields.

Required Fields with required

The simplest form of validation: making sure a field is filled before submission.

<form>
  <label for="name">Name (required):</label><br>
  <input type="text" id="name" name="name" required><br>
  <input type="submit" value="Submit">
</form>
Name (required): [              ]
[Submit]

Try submitting without typing anything — the browser will block it with a warning.

Pattern Matching with pattern

The pattern attribute uses regular expressions to match custom input formats.

<label for="fruit">Enter fruit name (only letters):</label><br>
<input type="text" id="fruit" name="fruit" pattern="[A-Za-z]+" required>
Enter fruit name (only letters): [             ]

This field only accepts alphabets. Numbers or symbols will trigger a validation error.

Using min and max Attributes

For numeric inputs, min and max define the valid range.

<label for="quantity">Choose a number between 1 and 5:</label><br>
<input type="number" id="quantity" name="quantity" min="1" max="5">
Choose a number between 1 and 5: [ ⬆️ ⬇️ ]

Trying to enter a value outside the range will prevent form submission.

Form action and method Revisited

Validation works best when you understand what happens on submit. These two attributes define that.

<form action="/process-form" method="post">
  <input type="text" name="name" required>
  <input type="submit" value="Go">
</form>
  • action: URL where the form data is sent
  • method: How the data is sent (get or post)

get shows form data in the URL, post keeps it hidden.

Grouping with <fieldset> and <legend>

Use <fieldset> to group related inputs together, and <legend> to provide a title for that group.

<fieldset>
  <legend>Fruit Preferences</legend>
  <input type="checkbox" id="apple" name="fruit" value="apple">
  <label for="apple">Apple</label><br>
  <input type="checkbox" id="banana" name="fruit" value="banana">
  <label for="banana">Banana</label>
</fieldset>
Fruit Preferences
☐ Apple  
☐ Banana

This helps create cleaner forms — both visually and semantically.

Full Example: Validated Fruit Survey

<form action="/submit-survey" method="post">

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

  <label for="age">Age (18 to 99):</label><br>
  <input type="number" id="age" name="age" min="18" max="99" required><br><br>

  <label for="fruit">Favorite Fruit (letters only):</label><br>
  <input type="text" id="fruit" name="fruit" pattern="[A-Za-z]+" required><br><br>

  <fieldset>
    <legend>Select at least one:</legend>
    <input type="checkbox" id="apple" name="fruits[]" value="apple">
    <label for="apple">Apple</label><br>
    <input type="checkbox" id="banana" name="fruits[]" value="banana">
    <label for="banana">Banana</label>
  </fieldset><br>

  <input type="submit" value="Submit">
  <input type="reset" value="Clear">
</form>
Name: [               ]
Age (18 to 99): [ ⬆️ ⬇️ ]
Favorite Fruit: [             ]

Select at least one:
☐ Apple  
☐ Banana  

[Submit] [Clear]

Best Practices

  • Always use required for critical fields
  • Use pattern for custom input validation
  • Apply min and max for numeric ranges
  • Wrap related inputs with <fieldset> for clarity
  • Label clearly and offer hints with placeholder

Summary

HTML form attributes offer a simple way to validate user input — with no JavaScript required. You’ve learned how to:

  • Use required, pattern, min, and max for field-level rules
  • Control form behavior with action and method
  • Group and label sections using fieldset and legend

Check your understanding 1

Which attribute is used to make an input field mandatory before form submission?
Choose one answer. You can try again.

Answer and explanation

<code>required</code>

The required attribute ensures the user cannot submit the form without filling that input field. It adds client-side validation with no JavaScript needed.

Check your understanding 2

The min and max attributes are only valid with number-based input types.
Choose one answer. You can try again.

Answer and explanation

True

Correct. min and max work with input types like number, range, and date to limit acceptable values or ranges.

Check your understanding 3

Choose the option that lists all and only the correct statements.

  1. required
  2. pattern
  3. maxlength
  4. optional
Which of the following are valid HTML input attributes for form validation?
Choose one answer. You can try again.

Answer and explanation

Statements 1 and 2 and 3

The first three are correct. required ensures input is filled, pattern allows regex matching, and maxlength limits character count. optional is not a valid HTML attribute.

Check your understanding 4

What is the purpose of using <fieldset> and <legend> in a form?
Choose one answer. You can try again.

Answer and explanation

To visually and semantically group related form elements

<fieldset> groups related elements, and <legend> acts as a caption for the group. This improves form organization and accessibility.

Check your understanding 5

Using pattern in an input field allows you to enforce specific character rules using regular expressions.
Choose one answer. You can try again.

Answer and explanation

True

Yes. For example, to restrict input to only 6-digit PIN codes, you can use: pattern="\d{6}". This ensures format validity before submission.

Check your understanding 6

Choose the option that lists all and only the correct statements.

  1. <input type="email" required>
  2. <input type="number" min="10" max="100">
  3. <input type="text" pattern="[A-Za-z]{3,}">
  4. <input type="text" optional>
Which of the following examples show correct use of form validation attributes?
Choose one answer. You can try again.

Answer and explanation

Statements 1 and 2 and 3

The first three are valid: they use required, min/max, and pattern. The last example uses optional, which is not recognized in standard HTML.

What’s Next?

Next, we’ll explore Custom Validation and Feedback — for creating even more helpful, interactive forms with CSS and JavaScript enhancements.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: HTML Required Fields →