How to check an input type="file" has a file or not using jquery?
Asked Answered
E

3

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?

Eleen answered 15/4, 2010 at 5:37 Comment(1)
Uhm, can you not just get the input element and then politely ask it for its "type" attribute?Pentatomic
A
17
if (jQuery('#File1').val()) { /* There are files */ }
Agate answered 15/4, 2010 at 5:45 Comment(0)
B
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/>
Bonds answered 18/1, 2013 at 5:25 Comment(1)
not working for me, form still got sent with an empty field.Hindoo
L
4
$('#upload').bind('click', function(e){
   if( $('#File1').val() != ""){
       // file selected
   }
   else{
       // no file selected
   }
});
Letourneau answered 15/4, 2010 at 5:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.