Jquery Validation File Upload

Code snippet below are for jquery validation file upload which file is not required, but when file is chosen, we need to check extension and size

jQuery.validator.addMethod("accept", function (value, element, param) 
{
   return value.match(new RegExp("." + param + "$"));
});

  $("input[type=file]").on("blur", function () {
        $("input[type=file]").each(function () {
         if ($(this).val() != '') {
             $(this).rules("add", {
              filesize: "1000000"
              accept: "png|jpe?g|doc|xls|ppt|docx|xlsx|pptx|pdf|tif|gif|bmp",
              required: false,
              messages: {
                    accept: "extension not allowed",
                    filesize: "file is too big"
                            
                        }
                    });
                }
            });
        });

        $("input[type=file]").valid(); //to trigger message automatically

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.