Home » Html » How to restrict <input type=“file”> so that it can only select .pdf files?

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

Posted by: admin November 30, 2017 Leave a comment

Questions:

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

Answers:

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.

Questions:
Answers:

more explicitly…

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

Questions:
Answers:

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.