Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restricting HTML file input to accept only .txt, and not .csv (in Chrome)

Tags:

html

text

csv

input

I tried to filter file format to accept only .txt in HTML. Here's my HTML code:

<Input
 type="file"
 accept="text/plain"
/>

In Safari, it works and only .txt files show up, but in Chrome (63.0.3239.84) the file selector also shows .csv files.

Is it possible to exclude .csv files in Chrome?

like image 233
jojomango Avatar asked Dec 28 '17 01:12

jojomango


People also ask

How do I restrict input type in HTML?

But it is possible to restrict the file types to only images, or certain image file extensions. To achieve this, you need to use the HTML accept attribute. This attribute is only used with <input type="file"> and serves as a filter to select file inputs from the file input dialog box.

How do you specify a file type in input?

The <input type="file"> defines a file-select field and a "Browse" button for file uploads. To define a file-select field that allows multiple files to be selected, add the multiple attribute. Tip: Always add the <label> tag for best accessibility practices!

How do you validate a file type in HTML?

For this we will use fileValidation() function. We will create fileValidation() function that contains the complete file type validation code. In this function we will use regex to check the type of file by the given pattern.

How do I restrict multiple file uploads in HTML?

Complete HTML/CSS Course 2022 To allow multiple file uploads in HTML forms, use the multiple attributes. The multiple attributes work with email and file input types. For limiting maximum items on multiple inputs, use JavaScript. Through this, limit the number of files to be uploaded.


1 Answers

The accept attribute specifies the types of files that the form input will accept.

Syntax

<input accept="file_extension|audio/*|video/*|image/*|media_type">

Tip: To specify more than one value, separate the values with a comma (e.g. .


Solution

Simply include the filetype you'd like to allow in the accept attribute, as follows:

<input accept=".txt"

Note: Because this file-restriction is client-side, users are able to remove this attribute and bypass this file-restriction, potentially leading to a vulnerability.

like image 145
Trent Avatar answered Sep 24 '22 00:09

Trent