Input [type = file] check

how can i check if the input file is empty?

I tried:

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

but it didn’t work.

+3
source share
1 answer

The event is clickfired before the value is set. Rather check it out during the event change.

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

Or, better, when submitting the form, because it changewill not be launched when the user has not selected anything, and the field itself was already empty.

$('#form').submit(function() {
    // ...
});
+6
source

Source: https://habr.com/ru/post/1752661/


All Articles