8

I have a file upload control <input id="File1" type="file" /> in my page... How to check an input type="file" has a file or not using jquery on click of a button upload?

1
  • Uhm, can you not just get the input element and then politely ask it for its "type" attribute?
    – user166390
    Commented Apr 15, 2010 at 5:38

3 Answers 3

17
if (jQuery('#File1').val()) { /* There are files */ }
9

You should use "required" instead of JQuery. By just one attribute it'll check input=file has file or not. Example:

<input type="file" required/>
1
  • not working for me, form still got sent with an empty field.
    – Nelson
    Commented Sep 24, 2019 at 23:07
4
$('#upload').bind('click', function(e){
   if( $('#File1').val() != ""){
       // file selected
   }
   else{
       // no file selected
   }
});

Not the answer you're looking for? Browse other questions tagged or ask your own question.