Cover Image for HTML 5 accept attribute
79 views

HTML 5 accept attribute

The accept attribute in HTML5 is used to specify the types of files that can be selected in a file input field (<input type="file">). It helps enforce restrictions on the file types that can be uploaded by the user.

The accept attribute can take one or more values, which can be file extensions or MIME types. Here are a few examples:

  1. Accepting a specific file extension:
HTML
<input type="file" accept=".jpg, .png, .gif">

In this example, only files with the extensions .jpg, .png, and .gif can be selected.

  1. Accepting files of a specific MIME type:
HTML
<input type="file" accept="image/jpeg, image/png">

In this case, only files of the MIME types image/jpeg and image/png can be selected.

  1. Accepting multiple file types:
HTML
<input type="file" accept=".pdf, .docx, application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document">

This example accepts files with the extensions .pdf and .docx, as well as files of the MIME types application/msword and application/vnd.openxmlformats-officedocument.wordprocessingml.document.

Please note that the accept attribute is only a hint to the browser and does not guarantee that the user will only be able to select files of the specified types. Users can still choose to upload files of any type, and server-side validation is necessary to ensure the uploaded files meet the desired criteria.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS