Check if input file is empty jquery

I have a form with file input, and I want to check if it is empty when the form is submitted.

I have this code:

$('form#new_comment').submit(function(e) { var $this = $(this); var $input = $this.find('input').val(); if($($input == '')) { alert ("you must choose a image"); return false; e.preventDefault(); } }); 

But he always says you must choose a image , even when I selected the image.

Where is the problem?

+6
source share
1 answer

Edit:

 if($($input == '')) { 

To:

 if($input == '') { 

Since this is a simple variable that contains text, you do not need to use the jQuery function again.

+9
source

Source: https://habr.com/ru/post/909168/


All Articles