👀 Live Preview
File input restricted to images with the accept attribute:

The accept attribute is a valuable feature in HTML that lets developers specify the types of files a user can select in a file input field. By defining accepted file types, you guide users to upload the correct files and improve form usability.
Limit what the picker shows.
Use .jpg, .png, .pdf.
Use image/* wildcards.
Where accept applies.
Set accept dynamically.
Never trust accept alone.
acceptThe primary purpose of the accept attribute is to define the types of files that are allowed to be selected through a file input element (<input type="file">). It helps users by filtering the displayed files in the file picker and indicating the expected file format.
accept improves the user experience but does not enforce file types. Users can still bypass the filter. Always validate uploads on the server.
Add accept to an <input type="file"> element with a comma-separated list of file type specifiers:
<input type="file" accept=".jpg, .jpeg, .png"><input type="file"> elements..) or MIME types.multipart/form-data on the parent <form> when uploading files.The accept attribute accepts one or more file type specifications, each separated by a comma. Specifications can include file extensions or MIME types:
accept=".jpg, .jpeg, .png" — Allows only image files with the specified extensions.accept="image/*" — Accepts any image file regardless of the specific extension.accept="application/pdf" — Accepts PDF documents by MIME type.accept="video/*,audio/*" — Multiple MIME type groups in one attribute.<!-- Extensions -->
<input type="file" accept=".pdf, .docx">
<!-- MIME wildcards -->
<input type="file" accept="image/*">
<!-- Specific MIME -->
<input type="file" accept="image/png, image/jpeg">| Use Case | accept Value | Notes |
|---|---|---|
| JPEG/PNG images | .jpg, .jpeg, .png | Extension-based filter |
| Any image | image/* | MIME wildcard |
| PDF documents | .pdf or application/pdf | Either form works |
| Audio files | audio/* | MP3, WAV, etc. |
| Video files | video/* | MP4, WebM, etc. |
| Applicable element | input type="file" | Only file inputs |
| Element | Supported? | Notes |
|---|---|---|
<input type="file"> | Yes | Primary and only standard use |
<input type="text"> | No | accept has no effect |
<form> | No | Use on the file input inside the form |
Image uploads, MIME type wildcards, and dynamic accept values with JavaScript.
File input restricted to images with the accept attribute:
Let’s look at an example of how to use the accept attribute in an HTML form:
<form>
<label for="fileInput">Select an image file:</label>
<input type="file" id="fileInput" name="fileInput" accept=".jpg, .jpeg, .png">
<input type="submit" value="Upload">
</form>In this example, the accept attribute specifies that only files with the extensions .jpg, .jpeg, and .png are allowed to be selected.
Accept any image format using the image/* MIME type wildcard:
<input type="file" accept="image/*">The image/* value tells the browser to filter for any file with an image MIME type, including PNG, JPEG, GIF, WebP, and SVG.
You can dynamically set the accept attribute using JavaScript to provide a more interactive and customized user experience:
<input type="file" id="dynamicFileInput">
<script>
document.getElementById("dynamicFileInput").accept = ".pdf, .docx";
</script>In this script, the accept attribute is set to allow only PDF and Word document files for an input with the id dynamicFileInput. This is useful when you want to adjust accepted file types based on user selections or form state.
<label for="..."> so users know what file type is expected.accept alone; add helper text like “PNG or JPG only, max 5 MB.”Define extensions or MIME types in HTML or JavaScript.
The browser filters visible files to match the accept list.
Server must re-validate type, size, and content independently.
Users pick the right files; your server enforces the rules.
The accept attribute is supported in all modern browsers on file inputs. Filtering behavior may vary slightly by operating system file picker.
All major browsers honor accept on input type="file" to guide file selection.
Bottom line: Use accept confidently on file inputs; always pair with server-side validation.
accept to filter unwanted files and improve UXenctype="multipart/form-data" on upload formsaccept as your only validation layeraccept on non-file input typesThe accept attribute is a valuable tool for controlling the types of files that users can upload through file input elements in HTML. By using this attribute judiciously, you can create forms that are more user-friendly and efficient.
Remember that accept is a helpful guide for users, but it does not enforce server-side validation. Always validate file types on the server to ensure security.
acceptBookmark these before your next file upload form.
Works on type=file.
Scope.jpg, .png, .pdf
Valuesimage/* wildcards
ValuesSet accept at runtime
DynamicNever trust accept alone
Security<input type="file"> is the standard element. Other input types ignore accept..png filters by extension. image/* accepts any image MIME type regardless of extension.accept is a client-side hint only. Always validate file type and content on the server.inputElement.accept based on form state or user choices.Practice the accept attribute with image and document filters in the Try It editor.
5 people found this page helpful