Ben
Ben

Reputation: 2564

Input file onchange check user has selected file

function preview(){
   if(...){//if user have select file
      //do something if input file onchange.
   }else{
      alert('please select a file');
   }
}

<input type='file' name='file' onchange='preview(this);' />

I have an input file, onchange will do something.

However I need to check if user has select the file.

Is anyway to check input file is not empty?

Upvotes: 0

Views: 2096

Answers (1)

Arindam Nayak
Arindam Nayak

Reputation: 7462

Here is my jsfiddle for this -- http://jsfiddle.net/Arindamnayak/estqvh9q/

<input type="file" id="fl" name="fl" onchange="preview()" />

function preview()
{
    var dc = document.getElementById("fl").files;
    if(dc.length == 0)
        alert("no file selected");
}

Upvotes: 3

Related Questions