HTML accept Attribute

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: 

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

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.

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:  


Contact Us