How to check if input file is empty in jQuery/JavaScipt

How to check if input file is empty in jQuery/JavaScipt

Today we are going to check if the file input element is empty when submitting the form with jQuery/JavaScript. In this post we are going to checking input type=file empty or not using jquery/JavaScript and how to get value of if any file selected.

Also Read

1. Get Checked CheckBox Value
2. Scroll to the top of page jquery

Checking if the file input is empty with JavaScript

Below code showing how to validate input type file if empty or selected if selected then how to get name of selected file using ID of the input field.

if( document.getElementById("fileFieldId").files.length == 0 ){
	alert("Please Attach File");
}
else{
	var fileName = document.getElementById("fileFieldId").files[0].name;
}

Validate if the file input is empty with Jquery

Below code showing how to validate input type file if empty or selected if selected then how to get name of selected file using ID of the input field.

if (jQuery('#fileFieldId').val() == '')
{
	alert("Please Attach File");
}else {
	var fileName = $("#fileFieldId").value;
}
About Author

My name is Mukesh Jakhar and I am a Web Application Developer and Software Developer, currently living in Jaipur, India. I have a Master of Computer Application in Computer Science from JNU Jaipur University. I loves to write on technology and programming topics. Apart from this, I love to travel and enjoy the beauty of nature.

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.