HTML Β· Chapter 36 of 45

HTML Form Attributes

Beyond action and method, forms and fields support attributes like required, placeholder, autocomplete, novalidate, and enctype that control validation and behavior.

The enctype attribute matters especially for file uploads β€” it must be set to multipart/form-data for a form containing a file input to work correctly.

Syntax
<form enctype="multipart/form-data">

Validation attributes

required makes a field mandatory before submission. pattern applies a regex constraint. novalidate on the form disables built-in browser validation entirely.

enctype for file uploads

When a form includes <input type="file">, you must set enctype="multipart/form-data" on the <form> tag, otherwise the file won't upload correctly.

Example 1 (html)
<input type="text" required placeholder="Enter your name">
Output
(shows placeholder text; blocks submission if empty)

required and placeholder improve both validation and usability.

Example 2 (html)
<form action="/upload" method="post" enctype="multipart/form-data">
  <input type="file" name="document">
</form>
Output
(correctly uploads the selected file)

multipart/form-data encoding is required for file uploads to work.

Key points

  • required prevents submission of empty mandatory fields.
  • placeholder shows hint text inside an empty field.
  • novalidate disables the browser's built-in validation.
  • enctype="multipart/form-data" is required for file uploads.
πŸ’‘ Note: placeholder text is not a substitute for a proper <label> β€” always include labels for accessibility.

πŸ“ Quick Quiz

1. Which attribute makes a field mandatory?

2. What enctype value is needed for file uploads?

3. What does novalidate do?