Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Check Input File

I have file input fields in a group like below. I'd like all of them to be required fields.

<!-- file upload group -->
<div class="Fieldset FileUpGroup">
  <span class="Legend">File Upload Group: (required)</span>
  <input name="fileUploads[]" type="file">
  <input name="fileUploads[]" type="file">
  <input name="fileUploads[]" type="file">
</div>

I have the following JQuery to validate, but it only validates the first one.

$('.FileUpGroup').each(function() {
    if($(this).find('input[type=file]').val() == '') { 
        Response('- Upload file not selected!', true);
        $(this).addClass('Error').fadeOut().fadeIn();
        return false;
    }
    else {
        $(this).removeClass('Error');
    }
});

Thank You!

like image 753
user1108996 Avatar asked Dec 25 '11 07:12

user1108996


1 Answers

You're using each() on the wrong element:

$('input[type="file"]').each(function() {
    var $this = $(this);
    if ($this.val() == '') { 
        Response('- Upload file not selected!', true);
        $this.addClass('Error').fadeOut().fadeIn();
        return false;
    } else {
        $this.removeClass('Error');
    }
});
like image 167
AlienWebguy Avatar answered Oct 24 '22 01:10

AlienWebguy