accept
accept attribute
The accept
attribute in HTML allows multiple file types for upload, separated by commas.
Syntax
<input accept="file_extension | audio/* | video/* | image/* | media_type">
Example
Note: Because of security issues, this example will not allow you to upload files.
<h1>The input accept attribute</h1>
<form>
<label for="img">Select image:</label>
<input type="file" id="img" name="img" accept="image/*">
<input type="submit">
</form>
<p><strong>Note:</strong> Because of security issues, this example will not allow you to upload files.</p>
accept
Attribute Overview of the
The accept
attribute in HTML tells <input>
elements of type file which file types to allow for upload.
accept
attribute is applicable only to <input type="file">
.Key Points
Purpose: Tells file input fields which file types to accept.
Usage: Use a comma-separated list of file extensions, MIME types, or wildcards like image
.
Important Considerations
- File Validation: The
accept
attribute doesn't guarantee file type correctness. Use server-side validation. - Multiple Selections: Use the
multiple
attribute to let users choose multiple files. - Unique File Type Specifiers: Include file extensions, MIME types, or wildcards like
<audio>
,<video>
,<img>
.
See also
- Using files from web applications
- File API
Conclusion
The accept
attribute in HTML allows you to restrict the types of files users can upload, enhancing the user experience by limiting file selection to specific formats. While it simplifies validation on the client side, server-side checks are essential for security. It can be applied to various file formats, extensions, and MIME types for different use cases.
abbr
The abbr attribute in HTML provides a way to shorten the text within <th> elements, improving the accessibility of tables. It is especially useful for long phrases, helping screen readers to convey the information more clearly.
alt
The alt attribute in HTML offers alternative text for images, enhancing web accessibility and boosting SEO. It describes images for users with visual impairments and is displayed when images fail to load.