12

My HTML looks like this:

<form>
    <input type="file" name="txtFile" pattern="?" id="txtFile" class="required"/>
</form>

pattern = '?'

Using which regular expression I would add the validation for the ONLY CSV FILE ALLOW.

If I upload .xls or any another file then it will display an error.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vijay Chauhan
  • 179
  • 1
  • 1
  • 7
  • you need javascript/jquery code to validate – Disha V. Dec 21 '15 at 07:28
  • @Disha : yes , it should be possible using js , but i have to create using html5.. – Vijay Chauhan Dec 21 '15 at 07:31
  • @Disha: ahan nice editing – devpro Dec 21 '15 at 07:44
  • @Disha: I suspect that this isolated closing tag is more often used than any other (obviously in non-HTML text, or should be). I can’t prove it — Google, Yahoo search, and Bing all ignore the non-alphabetics when told to search for — but that’s how ISTM. – devpro Dec 21 '15 at 07:55

4 Answers4

34

Now you can use the new HTML5 input validation attribute:

pattern="^.+\.(xlsx|xls|csv)$"

Accept type for other files (Reference: HTML5 Documentation):

For CSV:

<input type="file" accept=".csv" />

For Excel files, 2003-2007 (.xls):

<input type="file" accept="application/vnd.ms-excel" />

For Excel files, 2010 (.xlsx):

<input type="file" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />

For text files (.txt):

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

For image files (.png, .jpg, etc.):

<input type="file" accept="image/*" />

For HTML files (.htm, .html):

<input type="file" accept="text/html" />

For video files (.avi, .mpg, .mpeg, .mp4):

<input type="file" accept="video/*" />

For audio files (.mp3, .wav, etc.):

<input type="file" accept="audio/*" />

For PDF files, use:

<input type="file" accept=".pdf" /> 
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
devpro
  • 16,184
  • 3
  • 27
  • 38
8

It is not easy to accept just one of the file types. It depends on different OS and installed text reading programmes. In Unix type systems if you pass .csv file you will get .csv file type, but if you will pass the same file on Windows you will ger different file types, such as text/comma-separated-values, text/csv, application/csv, application/excel, application/vnd.ms-excel, application/vnd.msexcel, text/anytext.

It calls mime-types and can be different for any of text type files. Here is the link where you can read more.

Volodymyr Khmil
  • 1,078
  • 15
  • 13
6

Use this:

<input type="file" name="txtFile" id="txtFile" class="required" accept=".csv,text/csv" />

Mentioning the MIME type is good practice.

Dray
  • 887
  • 8
  • 25
4

Try this:

<input type="file" name="txtFile" accept=".csv" id="txtFile" class="required" />
aceraven777
  • 4,358
  • 3
  • 31
  • 55