HTML attribute: accept - HTML: HyperText Markup Language | MDN (2024)

The accept attribute takes as its value a comma-separated list of one or more file types, or unique file type specifiers, describing which file types to allow.

Try it

Overview

The accept property is an attribute of the file <input> type. It was supported on the <form> element, but was removed in favor of file.

Because a given file type may be identified in more than one manner, it's useful to provide a thorough set of type specifiers when you need files of specific type, or use the wild card to denote a type of any format is acceptable.

For instance, there are a number of ways Microsoft Word files can be identified, so a site that accepts Word files might use an <input> like this:

html

<input type="file" id="docpicker" accept=".doc,.docx,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document" />

Whereas if you're accepting a media file, you may want to be include any format of that media type:

html

<input type="file" id="soundFile" accept="audio/*" /><input type="file" id="videoFile" accept="video/*" /><input type="file" id="imageFile" accept="image/*" />

The accept attribute doesn't validate the types of the selected files; it provides hints for browsers to guide users towards selecting the correct file types. It is still possible (in most cases) for users to toggle an option in the file chooser that makes it possible to override this and select any file they wish, and then choose incorrect file types.

Because of this, you should make sure that expected requirement is validated server-side.

Examples

When set on a file input type, the native file picker that opens up should only enable selecting files of the correct file type. Most operating systems lighten the files that don't match the criteria and aren't selectable.

html

<p> <label for="soundFile">Select an audio file:</label> <input type="file" id="soundFile" accept="audio/*" /></p><p> <label for="videoFile">Select a video file:</label> <input type="file" id="videoFile" accept="video/*" /></p><p> <label for="imageFile">Select some images:</label> <input type="file" id="imageFile" accept="image/*" multiple /></p>

Note the last example allows you to select multiple images. See the multiple attribute for more information.

Unique file type specifiers

A unique file type specifier is a string that describes a type of file that may be selected by the user in an <input> element of type file. Each unique file type specifier may take one of the following forms:

  • A valid case-insensitive filename extension, starting with a period (".") character. For example: .jpg, .pdf, or .doc.
  • A valid MIME type string, with no extensions.
  • The string audio/* meaning "any audio file".
  • The string video/* meaning "any video file".
  • The string image/* meaning "any image file".

The accept attribute takes as its value a string containing one or more of these unique file type specifiers, separated by commas. For example, a file picker that needs content that can be presented as an image, including both standard image formats and PDF files, might look like this:

html

<input type="file" accept="image/*,.pdf" />

Using file inputs

A basic example

html

<form method="post" enctype="multipart/form-data"> <div> <label for="file">Choose file to upload</label> <input type="file" id="file" name="file" multiple /> </div> <div> <button>Submit</button> </div></form>
div { margin-bottom: 10px;}

This produces the following output:

Note: You can find this example on GitHub too — see the source code, and also see it running live.

Regardless of the user's device or operating system, the file input provides a button that opens up a file picker dialog that allows the user to choose a file.

Including the multiple attribute, as shown above, specifies that multiple files can be chosen at once. The user can choose multiple files from the file picker in any way that their chosen platform allows (e.g. by holding down Shift or Control, and then clicking). If you only want the user to choose a single file per <input>, omit the multiple attribute.

Limiting accepted file types

Often you won't want the user to be able to pick any arbitrary type of file; instead, you often want them to select files of a specific type or types. For example, if your file input lets users upload a profile picture, you probably want them to select web-compatible image formats, such as JPEG or PNG.

Acceptable file types can be specified with the accept attribute, which takes a comma-separated list of allowed file extensions or MIME types. Some examples:

  • accept="image/png" or accept=".png" — Accepts PNG files.
  • accept="image/png, image/jpeg" or accept=".png, .jpg, .jpeg" — Accept PNG or JPEG files.
  • accept="image/*" — Accept any file with an image/* MIME type. (Many mobile devices also let the user take a picture with the camera when this is used.)
  • accept=".doc,.docx,.xml,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document" — accept anything that smells like an MS Word document.

Let's look at a more complete example:

html

<form method="post" enctype="multipart/form-data"> <div> <label for="profile_pic">Choose file to upload</label> <input type="file" id="profile_pic" name="profile_pic" accept=".jpg, .jpeg, .png" /> </div> <div> <button>Submit</button> </div></form>
div { margin-bottom: 10px;}

Specifications

Specification
HTML Standard
# attr-input-accept

Browser compatibility

BCD tables only load in the browser

See also

  • Using files from web applications
  • File API
HTML attribute: accept - HTML: HyperText Markup Language | MDN (2024)

FAQs

What is the accept attribute in HTM? ›

Definition and Usage

The accept attribute specifies the types of files that the server accepts (that can be submitted through a file upload). Note: The accept attribute can only be used with <input type="file">. Tip: Do not use this attribute as a validation tool. File uploads should be validated on the server.

What file types are accept in HTML? ›

Acceptable file types can be specified with the accept attribute, which takes a comma-separated list of allowed file extensions or MIME types. Some examples: accept="image/png" or accept=".png" — Accepts PNG files. accept="image/png, image/jpeg" or accept=".png, .jpg, .jpeg" — Accept PNG or JPEG files.

What is the ID attribute in HTML? ›

The id attribute specifies a unique id for an HTML element. The value of the id attribute must be unique within the HTML document. The id attribute is used to point to a specific style declaration in a style sheet. It is also used by JavaScript to access and manipulate the element with the specific id.

How to accept user input in HTML? ›

Using HTML forms, you can easily take user input. The <form> tag is used to get user input, by adding the form elements. Different types of form elements include text input, radio button input, submit button, etc. Let's learn about the <input> tag, which helps you to take user input using the type attribute.

What is the use of accept charset in HTML? ›

The accept-charset attribute specifies the character encodings that are to be used for the form submission. The default value is the reserved string "UNKNOWN" (indicates that the encoding equals the encoding of the document containing the <form> element).

What is accept on input field? ›

The HTML <input> accept Attribute is used to specify the file types that the input field can accept, restricting the file selection to those specified types, such as image/* for images or . pdf for PDF files.

What is an HTML attribute with an example? ›

An HTML attribute is a piece of markup language used to adjust the behavior or display of an HTML element. For example, attributes can be used to change the color, size, or functionality of HTML elements. For more educational resources related to HTML attributes, please visit: How To Use HTML Attributes.

What characters are allowed in HTML ID attribute? ›

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

How to pass id in href in HTML? ›

Almost every HTML element takes the id attribute. So when you identify the portion of the web page you want to link to, assign it an id and then pass it to the href attribute as a value with the number symbol (#) preceding it.

Which HTML is used to accept user input? ›

The <input> tag specifies an input field where the user can enter data. The <input> element is the most important form element. The <input> element can be displayed in several ways, depending on the type attribute.

How to validate input value in HTML? ›

The simplest HTML validation feature is the required attribute. To make an input mandatory, add this attribute to the element. When this attribute is set, the element matches the :required UI pseudo-class and the form won't submit, displaying an error message on submission when the input is empty.

How to take text input in HTML? ›

The <input type="text"> defines a single-line text field. The default width of the text field is 20 characters. Tip: Always add the <label> tag for best accessibility practices!

What is the target attribute in HTM? ›

Definition and Usage. For <a> and <area> elements, the target attribute specifies where to open the linked document. For <base> elements, the target attribute specifies the default target for all hyperlinks and forms in the page.

What is allow attribute in iframe? ›

The allow attribute is used to enable the Geolocation API within the nested context. Here, an iframe is used to embed a player from a video site. The allowfullscreen attribute is needed to enable the player to show its video fullscreen.

What is Alt attribute in HTM? ›

The alt attribute specifies an alternate text for an area, if the image cannot be displayed. The alt attribute provides alternative information for an image if a user for some reason cannot view it (because of slow connection, an error in the src attribute, or if the user uses a screen reader).

What is the select tag in HTM? ›

Definition and Usage

The <select> element is used to create a drop-down list. The <select> element is most often used in a form, to collect user input. The name attribute is needed to reference the form data after the form is submitted (if you omit the name attribute, no data from the drop-down list will be submitted).

Top Articles
404 Error Page
8 Best Logging Practices to Keep Sensitive Data Out
7 C's of Communication | The Effective Communication Checklist
Dunhams Treestands
Diario Las Americas Rentas Hialeah
Rubratings Tampa
Walgreens Pharmqcy
Dricxzyoki
Noaa Swell Forecast
Nikki Catsouras Head Cut In Half
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Ukraine-Russia war: Latest updates
Cnnfn.com Markets
Local Collector Buying Old Motorcycles Z1 KZ900 KZ 900 KZ1000 Kawasaki - wanted - by dealer - sale - craigslist
Rainfall Map Oklahoma
Craigslist Malone New York
Www Craigslist Com Phx
Bcbs Prefix List Phone Numbers
Procore Championship 2024 - PGA TOUR Golf Leaderboard | ESPN
24 Hour Drive Thru Car Wash Near Me
CANNABIS ONLINE DISPENSARY Promo Code — $100 Off 2024
Craigslist Maui Garage Sale
Walmart Car Department Phone Number
Scream Queens Parents Guide
Employee Health Upmc
Mybiglots Net Associates
Slim Thug’s Wealth and Wellness: A Journey Beyond Music
Craig Woolard Net Worth
Water Temperature Robert Moses
Dal Tadka Recipe - Punjabi Dhaba Style
Publix Near 12401 International Drive
His Only Son Showtimes Near Marquee Cinemas - Wakefield 12
Japanese Emoticons Stars
R3Vlimited Forum
Strange World Showtimes Near Regal Edwards West Covina
Kstate Qualtrics
RUB MASSAGE AUSTIN
CARLY Thank You Notes
Flashscore.com Live Football Scores Livescore
Afspraak inzien
Facebook Marketplace Marrero La
Page 5662 – Christianity Today
2700 Yen To Usd
Amc.santa Anita
VDJdb in 2019: database extension, new analysis infrastructure and a T-cell receptor motif compendium
Tricare Dermatologists Near Me
Bmp 202 Blue Round Pill
Crystal Glassware Ebay
DL381 Delta Air Lines Estado de vuelo Hoy y Historial 2024 | Trip.com
Erica Mena Net Worth Forbes
Www Extramovies Com
Latest Posts
Article information

Author: Lilliana Bartoletti

Last Updated:

Views: 6109

Rating: 4.2 / 5 (53 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Lilliana Bartoletti

Birthday: 1999-11-18

Address: 58866 Tricia Spurs, North Melvinberg, HI 91346-3774

Phone: +50616620367928

Job: Real-Estate Liaison

Hobby: Graffiti, Astronomy, Handball, Magic, Origami, Fashion, Foreign language learning

Introduction: My name is Lilliana Bartoletti, I am a adventurous, pleasant, shiny, beautiful, handsome, zealous, tasty person who loves writing and wants to share my knowledge and understanding with you.