check if input file filed is empty jquery

24,946

Change:

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

To:

if($input == '') {

Since it is simple variable that holds text, you don't need to use jQuery function over it again.

Share:
24,946
hyperrjas
Author by

hyperrjas

Code Lover Ror, Nodejs, React/Redux, Mongodb/Mysql/GraphQl

Updated on July 09, 2022

Comments

  • hyperrjas
    hyperrjas almost 2 years

    I have a form with a 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 it always says you must choose a image, even when I have chosen an image.

    Where is the problem?

  • hyperrjas
    hyperrjas over 12 years
    The problem was fixed. I had bad id in input file field :D Thank you! Sarfraz
  • Sarfraz
    Sarfraz over 12 years
    @hyperrjas: You want to validate all inputs (because you have used $this.find('input') which gets all inputs ) or single field ?