Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check input file is empty, and also display an alert if png format is selected using jquery

I have got an input file upload , need to check which file is uploaded / if no file is selected by the client using jquery.

<input type="file" name="files" id="file1" style="color:White" />
<input type="submit" value="Create" />

The check should trigger when submit is clicked.

like image 843
Mr A Avatar asked Jul 13 '11 16:07

Mr A


1 Answers

See the sample I have created:

http://jsfiddle.net/G7xke/


<form>
<input type="file" name="files" id="file1" style="color:White" />
<input type="submit" value="Create" />
</form>
<button value="check" id="check" name="check" >check</button>

And JavaScript:

$("#check").click(function(){
    var fileName = $("#file1").val();
    if(fileName.lastIndexOf("png")===fileName.length-3)
        alert("OK");
    else
        alert("Not PNG");
})
like image 101
Aliostad Avatar answered Oct 19 '22 02:10

Aliostad