-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>
 ...
2
  • i have try to validate file using javascript but not able to do. Commented Mar 31, 2015 at 9:42
  • You can use jqueryvalidate plugin for this. Used it before it's pretty simple. jqueryvalidation.org Commented Mar 31, 2015 at 9:45

2 Answers 2

1

Try this.

<input name="file" type="file" accept="application/pdf" />
Sign up to request clarification or add additional context in comments.

2 Comments

as per your suggestion...you can change file type when file dialog box is open
I think there is no way to do validation in the client. You can check through extension only.
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;
    }
});

7 Comments

this only check the extension and not the filetype
@rikpg checking extension is better solution validation. There is no other way to check file type in Jquery.
the OP can use File API to do some stuff on client side, check this out: html5rocks.com/en/tutorials/file/dndfiles
your file validation is right..but when i select file except pdf..then file control is not created in div.
@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.
|

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.