-1

I have on file control that accept only pdf file. and i want to validate file that accept only pdf file as well as if another file is selected than recreate the file control in div tag with same name as well as same id.

validation must be javascript or jquery.

 ...
 <div>
        <input type="file" name="file" id="file1" />
 </div>
 ...
Amit Maru
  • 132
  • 7

2 Answers2

1

Try this.

<input name="file" type="file" accept="application/pdf" />
Aravind Sivam
  • 1,089
  • 8
  • 23
0

Try this:

$("#file1").change(function() {

    var val = $(this).val();

    switch(val.substring(val.lastIndexOf('.') + 1).toLowerCase()){
        case 'pdf':
            alert("Correct type");
            //your code
            break;
        default:
            // error message here
            alert("not a PDF file");
            //yourcode
            break;
    }
});
Manwal
  • 23,450
  • 12
  • 63
  • 93
  • this only check the extension and not the filetype – rvandoni Mar 31 '15 at 09:42
  • @rikpg checking extension is better solution validation. There is no other way to check file type in Jquery. – Manwal Mar 31 '15 at 09:45
  • the OP can use File API to do some stuff on client side, check this out: http://www.html5rocks.com/en/tutorials/file/dndfiles/ – rvandoni Mar 31 '15 at 09:47
  • your file validation is right..but when i select file except pdf..then file control is not created in div. – Amit Maru Mar 31 '15 at 09:47
  • @AmitMaru if you select file except pdf them `not a PDF file` alerts. Here you have to code for creating new file control, it will not create automatically. – Manwal Mar 31 '15 at 10:07
  • @Manwal ya right..but i dont know how to create file control using javascript if i am not select pdf file. – Amit Maru Mar 31 '15 at 10:19
  • See this http://stackoverflow.com/a/5656401/2236219. Same as this code you can create file input also. – Manwal Mar 31 '15 at 10:24