13

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.

4
  • you need javascript/jquery code to validate Commented Dec 21, 2015 at 7:28
  • @Disha : yes , it should be possible using js , but i have to create using html5.. Commented Dec 21, 2015 at 7:31
  • @Disha: ahan nice editing Commented Dec 21, 2015 at 7: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. Commented Dec 21, 2015 at 7:55

4 Answers 4

35

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" /> 
Sign up to request clarification or add additional context in comments.

2 Comments

@Disha: of course, reference mentioned. you can get online aswell
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.

Comments

6

Use this:

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

Mentioning the MIME type is good practice.

Comments

4

Try this:

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

1 Comment

Using the accept attribute you can validate any kind of file extension e.g. accept=".csv,.xls,.xlsx"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.