How to check input file is empty or not using JavaScript/jQuery ? Last Updated : 27 Dec, 2023 Comments Improve Suggest changes Like Article Like Report Given an HTML document containing an input element, the task is to check whether an input element is empty or not with the help of JavaScript. These are the two approaches to check input file is empty or not using JavaScript/jQuery: Table of Content Using element.files.length property in JavaScriptUsing element.files.length property in jQuery Approach 1: Using element.files.length property in JavaScriptTo check file is selected or not. If element.files.length property returns 0 then the file is not selected otherwise the file is selected. Example: This example implements the above approach. html <!DOCTYPE html> <html lang="en"> <head> <title>Check If Inputs are Empty using jQuery</title> <body> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <input type="file" name="File" id="file" /> <br><br> <button onclick="GFG_Fun()"> click here </button> <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> let up = document.getElementById('GFG_UP'); let down = document.getElementById('GFG_DOWN'); let file = document.getElementById("file"); up.innerHTML = "Click on the button to see" + " if any file is selected"; function GFG_Fun() { if (file.files.length == 0) { down.innerHTML = "No files selected"; } else { down.innerHTML = "Some file is selected"; } } </script> </body> </html> Output: Approach 2: Using element.files.length property in jQuery To check the file is selected or not. If element.files.length property returns 0 then the file is not selected otherwise file is selected. Example: This example implements the above approach. html <!DOCTYPE html> <html lang="en"> <head> <title>Check If Inputs are Empty using jQuery</title> <body> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <input type="file" name="File" id="file" /> <br><br> <button onclick="GFG_Fun()"> click here </button> <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> let up = document.getElementById('GFG_UP'); let down = document.getElementById('GFG_DOWN'); up.innerHTML = "Click on the button to see" + " if any file is selected"; function GFG_Fun() { if ($('#file')[0].files.length === 0) { down.innerHTML = "No files selected"; } else { down.innerHTML = "Some file is selected"; } } </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to check input file is empty or not using JavaScript/jQuery ? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JQuery JavaScript-Questions jQuery-Questions +1 More Similar Reads How to Check Mentioned File Exists or not using JavaScript/jQuery? Sometimes we want to upload a file through the path of the file, but a few times the file does not exist. In that case, we have to respond to users the given path does not find any file or does not exist in the mentioned file. Below are the approaches to check if mentioned file exists or not using J 3 min read How to Check an Object is Empty using JavaScript? These are the following ways that can be used to Check an Object is Empty using JavaScript: 1. Using Object.keys() Method - Mostly usedThe Object.keys() method returns an array that contains the property names of an object. If the length of array is 0, then object is empty.JavaScriptlet obj = {}; if 2 min read How to check an array is empty or not using jQuery ? In this article, we will check if an array is empty or not using jQuery. In JavaScript, arrays are a special type of object. If we use the typeof operator for arrays, it returns "object". We can use jQuery's isEmptyObject() method to check whether the array is empty or contains elements. The isEmpty 2 min read How to detect when cancel is clicked on file input using JavaScript ? This article describes the method to handle a situation when the user tries to input a file and later fails to upload the file. It gives the status to the user that the file is not uploaded. This may be useful in situations when an important file needs to be submitted or the application requires the 3 min read How to Check empty/undefined/null String in JavaScript? Empty strings contain no characters, while null strings have no value assigned. Checking for an empty, undefined, or null string in JavaScript involves verifying if the string is falsy or has a length of zero. Here are different approaches to check a string is empty or not.1. Using === OperatorUsing 2 min read Like