2

Need to check if a file input has a file attached.

This code doesn't work:

<input type="file" name="logo">

if ($('input[type=file]').length > 0) {
    if($(this).val() == '') {
        alert('Vide');
    }
    else {
        alert('Pas Vide');
    }
}

Any help please ?

1 Answer 1

1

The FileList object has a files property whose length you can check. See docs on FileList.

An object of this type is returned by the files property of the HTML <input> element

This SO answer addresses this quite well Possible duplicate ?

$("input:file").change(function (){
if ($('input[type=file]').get(0).files.length > 0) {
    if($(this).val() == '') {
        alert('Vide');
    }
    else {
        alert('Pas Vide');
    }
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="file" name="logo">