Briefly
The accept
attribute is added to the <input>
tag. It allows specifying which types of files the user needs to attach.
Example
Consider an example of a form where a user is asked to attach a picture of a cat:
<form method="post"> <label for="cat-picture">Attach a cat photo</label> <input type="file" id="cat-picture" name="cat-picture" accept=".jpg, .jpeg, .png" > <button>Attach</button></form>
<form method="post"> <label for="cat-picture">Attach a cat photo</label> <input type="file" id="cat-picture" name="cat-picture" accept=".jpg, .jpeg, .png" > <button>Attach</button> </form>
This form will only display files with the specified extensions in the file selection dialog.
How to Understand
The <input>
element with the accept
attribute limits the types of files that can be selected in the file selection dialog. This enhances the user experience by making the file selection process more convenient and less error-prone.
It is important to understand that the accept
attribute simply shows the user files of the types specified in the attribute's value when the dialog opens, but it does not filter them. File type checks should occur on the server side. Without this, nothing prevents a user from attaching a boring text document, even if you were expecting a kitty photo.
How to Write
The accept
attribute applies only to the <input>
element with type file
. It can accept the following values:
'audio
— accepts audio files of any format;/ *' 'video
— accepts video of any format;/ *' 'image
— accepts images of any format;/ *' 'image
— JPEG images;/ jpeg' 'image
— PNG images;/ png' 'application
— PDF documents;/ pdf' 'audio
— MP3 audio files;/ mpeg' 'video
— MP4 video files;/ mp4' - a MIME type string without extensions;
- file extensions preceded by a dot, e.g.,
.jpg
,.pdf
,.docx
, and so on.
You can specify multiple values, and you can also combine them:
<input type="file" name="cat-pic-video" accept="video/*, .jpg, .jpeg, .png">
<input type="file" name="cat-pic-video" accept="video/*, .jpg, .jpeg, .png" >
You can also combine MIME types and file extensions for greater flexibility:
<input type="file" name="documents" accept=" .pdf, application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document ">
<input type="file" name="documents" accept=" .pdf, application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document " >
The accept
attribute does not accept arbitrary strings that do not correspond to MIME formats or file extensions. For example, the value text
without specifying an extension or MIME type will be invalid.
Demonstration
Here you can feel how the browser behaves with extensions for all types of images, videos, and certain document extensions:
And here with certain image extensions:
Limitations
- Browser Support: Some older browsers may not fully support the
accept
attribute, especially when it comes to complex combinations of MIME types and file extensions. It is worth checking the attribute's support in specific browsers at Can I Use. - No Client-Side Filtering: The
accept
attribute does not prevent the upload of inappropriate files. It is merely a guideline for the file selection dialog. - Server-Side Validation Required: It is necessary to implement file type checks on the server side for security and proper application functionality.
Usage with Other Attributes
The multiple
attribute: allows the user to select multiple files.
<input type="file" name="cat-pictures" accept=".jpg, .jpeg, .png" multiple>
<input type="file" name="cat-pictures" accept=".jpg, .jpeg, .png" multiple >
The required
attribute: indicates that the field is mandatory.
<input type="file" name="cat-picture" accept=".jpg, .jpeg, .png" required>
<input type="file" name="cat-picture" accept=".jpg, .jpeg, .png" required >
Tips
💡 What to do if the user has several cats and wants to show you all of them? The multiple
attribute will help.
In practice
Advice 1
🛠️ To improve the user experience, you can use JavaScript to validate selected files before submitting the form.
Example:
document.getElementById('cat-picture').addEventListener('change', function() { const file = this.files[0] if (file && !file.type.match('image.*')) { alert('Please select an image.') this.value = '' }})
document.getElementById('cat-picture').addEventListener('change', function() { const file = this.files[0] if (file && !file.type.match('image.*')) { alert('Please select an image.') this.value = '' } })
🛠 In modern frameworks like React or Vue, you can integrate the accept
attribute directly into your component-based approach.
For example, in a React component, it might look like this:
function UploadForm() { return ( <form method="post"> <label htmlFor="cat-picture">Attach a cat photo</label> <input type="file" id="cat-picture" name="cat-picture" accept=".jpg, .jpeg, .png" /> <button>Attach</button> </form> );}
function UploadForm() { return ( <form method="post"> <label htmlFor="cat-picture">Attach a cat photo</label> <input type="file" id="cat-picture" name="cat-picture" accept=".jpg, .jpeg, .png" /> <button>Attach</button> </form> ); }