Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

input type="file" validation for contains file [duplicate]

Tags:

jquery

I'm using this html below:

<input name="t1" class="imgupload" type="file" accept="image/*" capture="camera">
<input type="submit" class="submit" value="Upload">

I'm trying to figure out how to have some alert(); show when the input type="file" is empty

$(document).on('click', '.submit', function(e) {
var check = $(".imgupload").val();
    if(check == 'undefined'){
       alert();
    }
});
like image 377
bogus Avatar asked Mar 26 '26 06:03

bogus


1 Answers

Here's an example

http://jsfiddle.net/aesmA/

HTML

<form>
  <input type="file" />
  <input type="submit" />
</form>

Javascript (jQuery)

$("form").on("submit", function(){
  var $file = $(this).find("input[type=file]");
  if (!$file.val() || $file.val() == "") {
    alert("File is missing");
    return false;
  }
});
like image 65
Sagish Avatar answered Mar 27 '26 20:03

Sagish