fms
fms

Reputation: 2187

How to restrict <input type="file"> so that it can only select .pdf files?

By default it can select all type of files,how to restrict it so that it can only select .pdf files?

Upvotes: 34

Views: 66808

Answers (3)

Timmerz
Timmerz

Reputation: 6199

more explicitly...

<input type="file" accept="application/pdf" /> 

Upvotes: 80

David
David

Reputation: 218930

You can use the accept attribute on your form to suggest to the browser to restrict certain types. However, you'll want to re-validate in your server-side code to make sure. Never trust what the client sends you.

Upvotes: 29

Darin Dimitrov
Darin Dimitrov

Reputation: 1039000

Simply put: you can't using the plain html and javascript. The closest you can get is to test the file extension using javascript before submitting the form and show some error message to the user if it is other than .pdf. You might need to use some client side solution such as Flash upload controls if you want to achieve this.

Upvotes: 1

Related Questions