0

I have created a form that must pass validation via JQuery to see if the fields are not null. In my form, I have <input type="file" id="file"></input>. When I run the following code, it doesn't work. Can someone shed some light on this for me? Thanks!

$('#check').click(function(){
      $('#submit').hide(200);

        var fileVal = $('#file').val(); 
        if(fileVal=='') 
        { 
            alert("Empty input file"); 

        }
});

For those who want more, by "work" I mean that I do not get the alert specified when the "check" button is clicked.

EDIT: I spelled something else wrong in my code. >.< I was staring at the screen for 2 hours, so I came here, but thanks anyway.

3 Answers3

1

Check out the "nice simple way to check if a variable" in JavaScript has a value.

Essentially, you can do this:

if (typeof fileVal !== 'undefined' && fileVal !== null) {
   console.log("This is non-empty");
}
else{
    console.log("This is empty");
}

Also, since you're using jQuery, you might be able to get a way with:

$('#file').text();

Though, I'd recommend making your element's ID different than it's type, i.e. id="myFileInput"

Community
  • 1
  • 1
blong
  • 2,815
  • 8
  • 44
  • 110
0

Your selector is wrong. inputFile should be file

var fileVal = $('#file').val();

Also, you can use a self closing tag for your input.

<input type="file" id="file" />
mrtsherman
  • 39,342
  • 23
  • 87
  • 111
0

where is your #inputFile.i think that is
$('#file').val();

4b0
  • 21,981
  • 30
  • 95
  • 142