Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

input[type=file] validation

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.

like image 438
vaxxis Avatar asked Jun 10 '26 23:06

vaxxis


1 Answers

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() {
    // ...
});
like image 151
BalusC Avatar answered Jun 13 '26 23:06

BalusC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!