How to validate input file with jquery validation?

1

I'm trying to validate a input of type file with Jquery Validation.

Following the documentation, I do the following: My input:

<input class="form-control" id="ImagemUpload" name="ImagemUpload" type="file" value="">

and my script:

         $("#ImagemUpload").rules("add", {
                accept: "image/jpeg, image/pjpeg",
                messages: {
                    accept: "Formato inválido."
                }
            });

However, you are not validating and the following message is appearing in the console:

  

Uncaught TypeError: Can not read property 'call' of undefined

    
asked by anonymous 21.11.2014 / 13:10

1 answer

1

I was reading the documentation and found no error in your code.

What I discovered is that I need an extra file in addition to the jquery.validate.js that I found in one of the demos on the site .

It is also important to remember that this code must come after you have started .validate() .

Make a demo work. Note that you need jQuery, jquery.validate.js and additional-methods.js .

$('#form').validate();
$('#ImagemUpload').rules('add', {
    accept: "image/jpeg, image/pjpeg",
    messages: {
        accept: "Formato inválido."
    }
});

jsFiddle: link

    
24.11.2014 / 09:11