160

Brand new to JS.
I am trying to check if the file input element is empty when submitting the form with jQuery/JavaScript. I have gone through a bunch of solutions and nothing is working for me. I am trying to avoid the /c/fakepath (unless there is no other option)

<input type="file" name="videoFile" id="videoUploadFile" />

This does not work:

var vidFile = $("#videoUploadFile").value;

The only way I can get the filename is if I use the following:

var vidFile = document.getElementById("videoUploadFile").files[0].name;

If there is no file available the code throws an error:

cannot read property name of undefined

which makes sense because the array is not set. but I cannot figure out how to do any error handling with this.

How do I properly grab the file input element videoUploadFile, check if it's empty, throw an error message if it's empty?

3
  • 8
    Check .files.length? Commented Sep 11, 2014 at 17:45
  • you can also filter non-empty files if you have more than one on the form: var files = $('#formbody').find('input[type=file]').filter(function() { return $(this)[0].files.length > 0; }); Commented Dec 9, 2019 at 14:57
  • @PiotrKowalski Great snippet but I would suggest to use let over var. For more information about the differences between them see stackoverflow.com/questions/762011/… Commented Jun 3, 2022 at 9:17

7 Answers 7

264

Just check the length of files property, which is a FileList object contained on the input element

if( document.getElementById("videoUploadFile").files.length == 0 ){
    console.log("no files selected");
}
Sign up to request clarification or add additional context in comments.

2 Comments

( document.getElementById("videoUploadFile").files.length === 0 )
See no purpose in third = sign here, except for IDE checks.
201

Here is the jQuery version of it:

if ($('#videoUploadFile').get(0).files.length === 0) {
    console.log("No files selected.");
}

2 Comments

This answer is more appropriate as the OP asked for a JQuery solution.
$('#videoUploadFile').get(0) same as $('#videoUploadFile')[0] but perhaps get() does some sanity checks
29

To check whether the input file is empty or not by using the file length property, index should be specified like the following:

var vidFileLength = $("#videoUploadFile")[0].files.length;
if(vidFileLength === 0){
    alert("No file selected.");
}

Comments

10

I know I'm late to the party but I thought I'd add what I ended up using for this - which is to simply check if the file upload input does not contain a truthy value with the not operator & JQuery like so:

if (!$('#videoUploadFile').val()) {
  alert('Please Upload File');
}

Note that if this is in a form, you may also want to wrap it with the following handler to prevent the form from submitting:

$(document).on("click", ":submit", function (e) {

  if (!$('#videoUploadFile').val()) {
  e.preventDefault();
  alert('Please Upload File');
  }

}

Comments

7

Question: How to check File field is empty or not?

Answer: I have solved this issue using this Jquery code

//If your file Is Empty:
if ($('#videoUploadFile').val() == '') {
    $('#message').html("Please Attach File");
} else {
    alert('ok');
}

    
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
    <input type="file" id="videoUploadFile">
</form>
<br>
<br>
<div id="message"></div>

4 Comments

Further explanation on why this would work or what was wrong in the original question would help raise the quality of this answer.
@josh burgess this answer was how to check File is empty or not is another way to check your file when you submit form using jquery .
@JoshBurgess the OP is asking how to do this in jQuery. All other answers either use vanilla JS or a combo of JS and jQuery. This is the only one who uses jQuery exclusively. Maybe I'm picky, but I don't like to mix jQuery with vanilla JS unless I really have to.
@EduardLuca Answer is over two years old. It was flagged as low quality at the time. An explanation of the code is generally appreciated by the SO community, and the comment was meant as a helpful reminder to not just post code but to explain what the code is and why it works. Hope that helps clear things up.
3
  $("#customFile").change(function() { 
    var fileName = $("#customFile").val();  

    if(fileName) { // returns true if the string is not empty   
        $('.picture-selected').addClass('disable-inputs'); 
        $('#btn').removeClass('disabled');   
    } else { // no file was selected 
      $('.picture-selected').removeClass('disable-inputs'); 
      $('#btn').addClass('disabled');
    }  
  });

1 Comment

I think you should add some comments on your post.
3
if (data.name == '') {
   //file doesn't exist;
}
else {
   //file exist;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.