6

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?

1 Answer 1

9

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.