In this article I will explain with an example, how to restrict specific files only i.e. accept only specific type of files in HTML5 INPUT FileUpload element.
The accept attribute is a new attribute available in HTML5.
Note: Accept Attribute does not restrict user from uploading invalid file.
 
 
HTML Markup
The following HTML Markup consists of:
FileUpload – For selecting file.
The HTML5 INPUT FileUpload has been set with the following attribute.
accept – This attribute allows user to select only specified type of files from the Choose File dialog box.
Note: Accepting all files is default behavior, hence even if the accept attribute is not specified, by default, it will accept all files.
 
For example, in the following code, the HTML5 INPUT FileUpload will only allow user to select Word Documents i.e. files with extension doc and docx.
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <input type="file" accept=".doc, .docx" />
</body>
</html>
 
 
DO NOT use the accept attribute for validation
The accept attribute does not restrict user from uploading invalid files.
Restrict files selection in HTML5 FileUpload
 
And hence, if you want to restrict user and perform validation then please refer the following articles
 
 
Screenshot
Restrict files selection in HTML5 FileUpload
 
 
Browser Compatibility
The above code has been tested in the following browsers only in versions that support HTML5.
Microsoft Edge  FireFox  Chrome  Safari  Opera
* All browser logos displayed above are property of their respective owners.
 
 
Demo
 
 
Downloads