The HTML accept attribute
The HTML accept attribute lists the file types a file input should offer, such as image/* or .pdf. Browsers use it to narrow the file picker, but a file that doesn't match still counts as valid. The server has to check each file's type.
Overview
The HTML accept attribute names the file types that a file input is meant to take. It needs an <input> with type="file", and even there it is only a hint for the browser. The capture attribute builds on it to ask for a camera or microphone.
The value is a comma-separated list. Each entry is a file extension with a leading dot, such as .pdf, or a MIME type such as image/png, and the wildcards image/*, video/* and audio/* work too. A MIME type with parameters, such as text/plain;charset=utf-8, isn't allowed.
Browsers are expected to stop people from picking a file that matches no entry. In most cases the file chooser still has an option to show every file, so the list guides people rather than locking them in.
The reported type doesn't prove what a file holds. Chrome and Firefox take file.type from the file name, so a text file renamed notes.png arrives as image/png, and a PNG image renamed with .txt arrives as text/plain.
Syntax
<label for="photo">Photo (JPEG or PNG)</label>
<input type="file" id="photo" name="photo"
accept=".jpg,.jpeg,.png,image/jpeg,image/png">
Values
| Value |
|---|
A comma-separated list of extensions (.pdf), MIME types (image/png), or the image/*, video/* and audio/* wildcards. |
Example
<label for="photo">Profile photo (JPEG or PNG)</label><br>
<input type="file" id="photo" name="photo"
accept=".jpg,.jpeg,.png,image/jpeg,image/png">
<p id="msg" role="status" style="overflow-wrap:anywhere">No file chosen yet.</p>
<p>Choose a photo, or drag any other file onto the button, and see what the input took.</p>
<script>
const input = document.getElementById('photo');
const msg = document.getElementById('msg');
const tokens = input.accept.toLowerCase().split(',').map(t => t.trim());
input.addEventListener('change', function () {
const file = input.files[0];
if (!file) {
msg.textContent = 'No file chosen yet.';
return;
}
const name = file.name.toLowerCase();
const listed = tokens.some(t => t[0] === '.' ? name.endsWith(t) : t === file.type);
msg.textContent = file.name + ' (' + (file.type || 'no type') + ') ' +
(listed ? 'matches accept.' : 'is not in accept, and validity.valid is ' + input.validity.valid + '.');
});
</script>
Best practices
- Give
acceptboth the extension and the MIME type of each format, as in.png,image/png. Listing both helps, since some systems identify files by extension and others by MIME type. - Tell people the accepted formats before they open the picker, for example with a label such as "Photo (JPEG or PNG)". Most operating systems lighten files that don't match, and the label explains why those files look unavailable.
- Check every upload on the server by reading its content, not its name or reported type. Extensions tend to be ambiguous, and MIME types tend to be unreliable.
- Write each extension with its leading dot, as
.jpgrather thanjpg. Only extensions that start with a dot are valid, and validator.nu reports a barejpgas a bad value. - Put
accepton each file input, not on the <form>. On a form, accept is an obsolete feature.
Frequently asked questions
Which accept value offers only images?
image/* for any image file, or list only the formats you handle, such as .png,.jpg,image/png,image/jpeg. Either value only steers the file picker.Can users still upload other file types when accept is set?
image/* is taken, and a script can put any file into input.files.Does the accept attribute validate the selected file?
Does accept treat .PDF and .pdf as the same extension?
Does accept=audio/* work in Safari on iOS?
How do I limit file size if accept only covers types?
file.size in a change listener to warn people early, and enforce the limit on the server.






















