How to validate input file with jquery validation?

Asked

Viewed 2,767 times

1

I’m trying to validate a input of the kind 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, it is not validating and the following message is showing up on the console:

Uncaught Typeerror: Cannot read Property 'call' of Undefined

  • Can you do a jsFiddle with the problem? So we can test and help better. There are many validation plugins. It’s hard to know which one you’re using...

  • I am using http://jqueryvalidation.org/

1 answer

1


I’ve been reading the documentation and found no error in your code.

What I found is that you need an extra file in addition to jquery.validate.js that I found in one of the website’s demos.

It is also important to remember that this code of yours has to come after you have started the .validate().

There’s a demo working. 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: http://jsfiddle.net/pd08qtzL/

  • Additional-methods.js was missing, I hadn’t noticed that on the day, thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.