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.
<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.
<input type="text" required placeholder="Enter your name">(shows placeholder text; blocks submission if empty)required and placeholder improve both validation and usability.
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="document">
</form>(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.
