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

HTML accept Attribute

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

The HTML <input> accept attribute is used to control the type of files that can be selected for input, restricting the file selection to specific formats such as image/* for images or .pdf for PDF files.

Syntax:  

<input accept = "file_extension | audio/* | video/* | image/* | media_type">
HTML
<html>
  <body>
    <form>
      <label for="fileUpload">Upload an image file:</label>
      <input type="file" id="fileUpload" name="fileUpload" accept="image/*">
      <input type="submit" value="Submit">
    </form>
  </body>
</html>
  • The accept=”image/*” attribute restricts the file input to accept only image files, enhancing user experience by filtering selectable files in the dialog.
  • The <input type=”file”> element allows users to browse and select files from their device for upload.

Attribute Values:

ValueDescription

file_extension

Specify the file extension(s) like .gif, .jpg, .png, .doc) the user can pick from.

audio/*

The user can pick all sound files.

image/*

A valid media type, with no parameters. Look at IANA Media Types for a complete list of standard media types.

media_type

A valid media type without parameters.

More example of HTML <input> accept Attribute

HTML <input> accept Attribute

html
<html >
  <body>
    <form>
      <label for="imageUpload">Upload an image (JPEG or PNG):</label>
      <input type="file" id="imageUpload" name="imageUpload" accept=".jpeg, .jpg, .png">
      <input type="submit" value="Submit">
    </form>
  </body>
</html>
  • The accept attribute is set to .jpeg, .jpg, .png, allowing users to select image files with these specific extensions.
  • This ensures that only JPEG and PNG images can be selected for upload.

Styled File Input for PDF Documents

HTML
<html>
<head>
    <style>
        .file-input {
            display: inline-block;
            padding: 10px 20px;
            font-size: 16px;
            cursor: pointer;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
        }
        .file-input:hover {
            background-color: #45a049;
        }
        #pdfUpload {
            display: none;
        }
    </style>
</head>
<body>
    <form>
        <label for="pdfUpload" class="file-input">Choose a PDF file</label>
        <input type="file" id="pdfUpload" name="pdfUpload" accept="application/pdf">
        <input type="submit" value="Submit">
    </form>
</body>
</html>
  • The file input is styled to appear as a button labeled “Choose a PDF file,” enhancing the user interface.
  • The accept attribute is set to application/pdf, restricting file selection to PDF documents only.

Best Practices for Using the HTML <input> accept Attribute:

  • Specify Exact File Types: Clearly define acceptable file types using MIME types or file extensions to guide users in selecting appropriate files.
  • Implement Server-Side Validation: While the accept attribute provides a hint to users, always validate the file type on the server to ensure security and proper handling.
  • Provide User Instructions: Offer clear guidance or labels indicating the types of files accepted to enhance user experience and reduce errors.

HTML <input> accept Attribute – FAQs

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

To Specify acceptable file types for server upload using the “accept” attribute within the HTML <input type=”file”> element.

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

Here we create an input field that accepts CSV files by adding accept=”.csv” attribute to the HTML <input type=”file”> element.

What is the Limit File Format when using <input type=”file”> ?

The limit file format when using <input type=”file”> is defined by the “accept” attribute, specifying accepted file types.

Does the accept attribute enforce file type restrictions?

No, the accept attribute suggests acceptable file types to users but does not enforce restrictions. Users can still select files of other types, so server-side validation is essential.

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

No, the accept attribute is only applicable to <input type=”file”> elements and has no effect on other input types.

How do I specify multiple file types in the accept attribute?

List multiple MIME types or file extensions separated by commas within the accept attribute. For example:

<input type=”file” accept=”.jpg, .png, .pdf”>

Does 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.

Is the accept attribute supported in all browsers?

Most modern browsers support the accept attribute; however, behavior may vary, and it’s advisable to test across different browsers to ensure consistent functionality.


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!


Next Article

Similar Reads

three90RightbarBannerImg