30

I am trying to validate a HTML file upload using jQuery Validate

I found I can use the meta option like:

$("#myform").validate({
   meta: "validate"
})

<input type="file" name="upload" class="{validate:{required:true,accept:'docx?|pdf'}}" />

But it does not appear to work. I need to add the required class anyways. Then the file type check will not work too of course. Do I need to do something additional?

3
  • You can leave the required. But can you do the client-side validation on before submit? You also need to do validation on the server-side though. Commented Jun 2, 2011 at 10:13
  • ...what I mean is: create a jQuery script for .submit() so you can validate before submitting. Commented Jun 2, 2011 at 10:21
  • @Alex R., Actually thats in the form submit handler... is that the wrong way of doing this? Commented Jun 3, 2011 at 2:08

3 Answers 3

67

If you want to use jQuery's validate to check the size of the file, you can do so by doing the following :

1- load the additional methods file

<script src="http://cdn.jsdelivr.net/jquery.validation/1.15.0/additional-methods.min.js"></script>

2- add a custom method to validate the file size

$.validator.addMethod('filesize', function(value, element, param) {
    // param = size (in bytes) 
    // element = element to validate (<input>)
    // value = value of the element (file name)
    return this.optional(element) || (element.files[0].size <= param) 
});

3- use the newly added method as a validation rule to validate your input:

$('#formid').validate({
    rules: { inputimage: { required: true, extension: "png|jpe?g|gif", filesize: 1048576  }},
    messages: { inputimage: "File must be JPG, GIF or PNG, less than 1MB" }
});

Note: using the "accept" validation rule instead of "extension" result in MIME type error messages when you upload a file with a blank file type.

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

5 Comments

i get this error... e.validator.methods[o] is undefined [Break On This Error] ...,t;for(e=0;this.errorList[e];e++){var n=this.errorList[e];if(this.settings.highl...
Hmmm, if I make required: false it returns a a.validator.methods[d] is undefined error. It's not required to add a file to in my form.
You should include additional-method.js first, look like this <script src="http://cdn.jsdelivr.net/jquery.validation/1.15.0/additional-methods.min.js"></script>
When I add your method, it works fine, except after I re-choose another image with size less than the threshold, the hint message doesn't disappear automatically. I have to re-click submit to have the hint message disappear again. How could I solve that problem? Thanks!
Thanks for the Note. That's helped me to change the message
11

You can add a validation method to accomplish

    jQuery.validator.addMethod("uploadFile", function (val, element) {

          var size = element.files[0].size;
            console.log(size);

           if (size > 1048576)// checks the file more than 1 MB
           {
               console.log("returning false");
                return false;
           } else {
               console.log("returning true");
               return true;
           }

      }, "File type error");

And Use it like this -

      $(document).ready(function(){
              $('#quote_form').validate({
      rules: {
           image: {
              required: true,
              extension:'jpe?g,png',
              uploadFile:true,
              }
             }
          });
       });

I hope this will help you..

1 Comment

the uploadFile rule works, but the extension rule doesn't . Are you ok?
2

Have you read and tried it this way: http://docs.jquery.com/Plugins/Validation/Methods/accept#extension ?

I mean rather than adding the validate rules to the class have you tried to just instantly add them in validate init?

1 Comment

Good way to check MIME type. But we need to check the file extension

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.