How to check input file is empty or not using JavaScript/jQuery ?
Given an HTML document containing input element and the task is to check whether an input element is empty or not with the help of JavaScript.
Approach 1: Use element.files.length property to check 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.
<!DOCTYPE HTML> <html> <head> <title> How to check input file is empty or not using JavaScript? </title> </head> <body style = "text-align:center;"> <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> var up = document.getElementById('GFG_UP'); var down = document.getElementById('GFG_DOWN'); var 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:
- Before clicking on the button:
-
After clicking on the button:
Approach 2: Use 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.
<!DOCTYPE HTML> <html> <head> <title> JavaScript | Check if input file is empty. </title> <script src= </script> </head> <body style = "text-align:center;"> <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> var up = document.getElementById('GFG_UP'); var 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:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- HTML | <input type="file">
- How to get the input file size in jQuery ?
- Check if an array is empty or not in JavaScript
- How to check an object is empty using JavaScript?
- How to check an HTML element is empty using jQuery ?
- How to reset input type = "file" using JavaScript/jQuery?
- How to check empty/undefined/null string in JavaScript?
- JavaScript | WebAPI | File | File.size Property
- JavaScript | WebAPI | File | File.type Property
- JavaScript | WebAPI | File | File.name Property
- PHP | empty() Function
- CSS | :empty Selector
- D3.js | d3.set.empty() Function
- D3.js | d3.map.empty() Function
- jQuery | :empty Selector
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



