Sometimes, we want to check if file input is empty in JavaScript.
In this article, we’ll look at how to check if file input is empty in JavaScript.
How to check if file input is empty in JavaScript?
To check if file input is empty in JavaScript, we can get the files.length
property from the file input.
For instance, we write:
<input type='file' id='videoUploadFile'>
to add a file input.
Then we write:
if (document.getElementById("videoUploadFile").files.length === 0) {
console.log("no files selected");
}
We select the file input with document.getElementById
.
Then we use the files.length
property to check if any files are selected.
If it returns 0, then no file is selected.
Conclusion
To check if file input is empty in JavaScript, we can get the files.length
property from the file input.