How to make input type accpet only PDF, i have tried accept="application/pdf" in my html but it still accepts pptx, xlsx, doc, etc.
Asked
Active
Viewed 914 times
-2
-
1You should always use server side validation for things like this too. Just in case you weren't. – powerbuoy Aug 27 '18 at 17:50
-
how do i do this using KnockoutJs?? – Nik Aug 27 '18 at 18:02
2 Answers
0
The accept attribute is only supported on some browsers, not all. I would write some jquery to check the file extension once the user has uploaded their file. If the extension is different than what you specify, then clear the field and throw an alert.
I do not have an example handy for you but it is not difficult to achieve.
check this SO question for ways to check the extension.

Matthew
- 922
- 1
- 6
- 21
-
-
I do not use KnockoutJS so I am not familiar with it. It looks like it supports vanilla JavaScript though. [This answer](https://stackoverflow.com/a/1203361/2675902) may help you with a vanilla JS version. – Matthew Aug 27 '18 at 18:09
0
try this:
<input type='file' accept='.pdf' />
several extensions:
<input type='file' accept='.pdf,.doc,.ppt' />

MSKashef
- 344
- 2
- 11