3

how can I check if the input file is not empty?

I tried:

$('#image-file').click(function() { if ( ! $('#image-file').val() ) { alert('Chose a file!'); return false; } }); 

but it didn't work.

1 Answer 1

6

The click event is fired before the value is been set. Rather check it during change event.

$('#image-file').change(function() { // ... }); 

Or, better, during submitting of the form, because the change won't be fired when the user selected nothing and the field itself was already empty.

$('#form').submit(function() { // ... }); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.