Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate a file upload field using Javascript/jquery

Tags:

How can I validate if the user has selected a file to upload?

Edit: bumped

like image 314
Ali Avatar asked May 07 '09 05:05

Ali


People also ask

How do you check if file is uploaded or not in JavaScript?

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.

How do you validate a file type in JavaScript?

Using JavaScript, you can easily check the selected file extension with allowed file extensions and can restrict the user to upload only the allowed file types. For this we will use fileValidation() function. We will create fileValidation() function that contains the complete file type validation code.

How can we make mandatory file upload in jQuery?

No need of JavaScript validation you can use "required" attribute in input type.... The HTML5 required attribute marks any input/textarea as being required to have a value before the form can be submitted.


1 Answers

Check it's value property:

In jQuery (since your tag mentions it):

$('#fileInput').val() 

Or in vanilla JavaScript:

document.getElementById('myFileInput').value 
like image 104
Sietse Avatar answered Oct 13 '22 01:10

Sietse