Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I know if my input type="file" has content selected

Tags:

i have a simple input file <input type="file" name="file" id="file"> but I'm trying to validate after the post if the input file has content.

I trying with $("#file").val() but, the dom of the controls is the same always.

like image 200
Benjamin RD Avatar asked Apr 20 '13 21:04

Benjamin RD


People also ask

How do you check input type file is empty or not?

We get our input element with document. getElementById() and check the length of the files property, which an input of type file has. If it's empty, we alert the user that no file has been selected.

How do you check file is selected or not in JS?

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.

How do I select an input type file?

The <input type="file"> defines a file-select field and a "Browse" button for file uploads. To define a file-select field that allows multiple files to be selected, add the multiple attribute. Tip: Always add the <label> tag for best accessibility practices!

Which input type is used to select the file from device storage?

The <input> element with type "file" is used to select one or more files from user device storage. Once you select the file, and after submission, this file can be uploaded to the server with the help of JS code and file API.


2 Answers

What do you mean by: after the post, do you mean:

-After clicking on a submit button but before posting or submitting?

-Or you mean after it has been submitted and the content has reached the server?

If it is the first case, make sure to return false to the form so it doesn't submit, and you can validate, for example:

<form onsubmit="return false"> 

or try to return a boolean to the form after calling a validation function on submit:

<form onsubmit="return validate()"> 

Then using Jquery validate your data:

function validate(){    valid = true;       if($("#file").val() == ''){          // your validation error action         valid = false;       }      return valid //true or false } 

if you find errors return false, if the data is valid return true. After understanding how it works, ultimately, all of this can be done with Jquery:

HTML:

<form id="fileform" action="nextpage.php"> 

JS:

$('#fileform').submit(function(){      valid = true;       if($("#file").val() == ''){         // your error validation action         valid =  false;     }      return valid }); 

Also, if you are checking to see what type of file is being submitted, you can check this post: jQuery - INPUT type=File , Image FileType Validation options?

If what you want to do is validate the data AFTER submitting then you have to code the validation in a server scripting language like PHP.

like image 73
multimediaxp Avatar answered Sep 19 '22 19:09

multimediaxp


try to get the value after the form submission like this

$('#yourform').submit(function(){      var val = $("#file").val();     if(val == ''){        return false;     } }); 
like image 22
Mohamed Nagy Avatar answered Sep 17 '22 19:09

Mohamed Nagy