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
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; }
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; }
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.