The Wayback Machine - https://web.archive.org/web/20250114191915/https://www.geeksforgeeks.org/html-accept-attribute/
Open In App

HTML accept Attribute

Last Updated : 27 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

HTML accept Attribute specifies the type of file that the server accepts. This attribute can be used with <input> element only. This attribute is not used for validation tools because file uploads should be validated on the Server.

Syntax:

<input accept = "file_extension"> 

Note: This attribute is not supported in HTML5.

Attribute Values:

TypeDescription
file_extensionIt Specify the file extension(s) like .gif, .jpg, .png, .doc) the user can pick from.
audio/*The user can pick all sound files.
video/*The user can pick all video files.
image/*A valid media type, with no parameters. Look at IANA Media Types for a complete list of standard media types.
media_typeA valid media type without parameters

Attribute: The accept attribute is associated with <input> elements only. 

HTML accept Attribute Examples

Example: Here is the implementation of HTML accept Attribute.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>accept attribute</title>
    <style>
        body {
            text-align: center;
        }
    </style>
</head>

<body>
  
    <form action=" ">
        <input type="file" name="picture" 
               accept="image/*">
        <input type="submit">
    </form>
</body>

</html>

Output: 

HTML accept attribute example output

Explanation:

  • In the above example we creates a form with the action attribute set to an empty string.
  • Inside the form, an input field of type file is defined with the name attribute set to “picture”.
  • The accept attribute specifies that only image files are accepted for upload.
  • A submit button is provided to submit the form data.

HTML accept Attribute Use cases

1. How to specify the type of files that server accepts in HTML5 ?

In HTML5, the accept attribute is used within the input element of type file to specify the types of files that the server can accept for upload.

2. How to create input field that accept CSV file in HTML ?

To create an input field that accepts CSV files in HTML, use the input element with type=”file” and set the accept attribute to “.csv” or “text/csv”.

Supported Browsers

The browser supported by accept attribute are listed below:  

HTML accept Attribute – FAQs

Where is the accept attribute used?

The accept attribute is used specifically in <input type=”file”> elements. It restricts the file types that can be chosen by the user.

What happens if a user tries to select a file type not allowed by the accept attribute?

The file selection dialog will filter out files that do not match the specified types, preventing the user from selecting them. However, the validation isn’t foolproof, and server-side checks should be used as a backup.

Can the accept attribute restrict file size?

No, the accept attribute only restricts file types, not file sizes. To enforce file size limits, server-side validation is required.

Can the accept attribute be used with input types other than file?

No, the accept attribute is only applicable to <input type=”file”>. It does not work with other input types like text, radio, or checkbox.

Can users still bypass the accept attribute restrictions?

Yes, technically, the accept attribute only provides a suggestion to the browser, and users can bypass it by manually changing file extensions or using developer tools. Therefore, it is crucial to validate file types server-side after the file is uploaded.

Dive deep into the world of JavaScript with our JavaScript Full Course Online, offering everything from the basics to advanced techniques. Learn how to build interactive websites, optimize your code, and earn a certification to showcase your skills.

Take the Three 90 Challenge! Complete 90% of the course in 90 days, and earn a 90% refund. Push your limits and stay motivated as you work towards mastering JavaScript. Join now and get started on your JavaScript learning journey!


Similar Reads

three90RightbarBannerImg