views:

633

answers:

2

How can I validate if the user has selected a file to upload?

Edit: bumped

A: 

In Firefox at least, the DOM inspector is telling me that the File input elements have a property called files. You should be able to check its length.

document.getElementById('myFileInput').files.length
nickf
Doesn't work in chrome so probably wont work in IE either
Click Upvote
+2  A: 

Check it's value property:

In jQuery (since your tag mentions it):

$('#fileInput').get(0).value

Or in vanilla JavaScript:

document.getElementById('myFileInput').value
Sietse