
HTML 5 accept attribute
The accept
attribute in HTML5 is used to specify the types of files that can be selected in a file input field (<input type="file">
). It helps enforce restrictions on the file types that can be uploaded by the user.
The accept
attribute can take one or more values, which can be file extensions or MIME types. Here are a few examples:
- Accepting a specific file extension:
<input type="file" accept=".jpg, .png, .gif">
In this example, only files with the extensions .jpg
, .png
, and .gif
can be selected.
- Accepting files of a specific MIME type:
<input type="file" accept="image/jpeg, image/png">
In this case, only files of the MIME types image/jpeg
and image/png
can be selected.
- Accepting multiple file types:
<input type="file" accept=".pdf, .docx, application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document">
This example accepts files with the extensions .pdf
and .docx
, as well as files of the MIME types application/msword
and application/vnd.openxmlformats-officedocument.wordprocessingml.document
.
Please note that the accept
attribute is only a hint to the browser and does not guarantee that the user will only be able to select files of the specified types. Users can still choose to upload files of any type, and server-side validation is necessary to ensure the uploaded files meet the desired criteria.