jQuery-Validation-Engine - Validation File MIME Type

Kashuda picture Kashuda · Jun 13, 2013 · Viewed 7.7k times · Source

I would like to use the jQuery-Validation-Engine to validate the MIME Type of a file input. How could I do this?

My approach would be something like this:

function checkMIME(field, rules, i, options){
    var file = $("#userfile")[0].files[0];
    var MimeFilter = /^(image\/bmp|image\/gif|image\/jpeg|image\/png|image\/tiff)$/i;
    if (! MimeFilter.test(file.type)) {
     return options.allrules.validate2fields.alertText;
  }
}

Thanx Kashuda

Answer

Kashuda picture Kashuda · Jun 15, 2013

OK, found a solution by adding these lines to the query.validationEngine-LANG.js:

"validateMIME":  {
    "func": function(field, rules, i, options){
   //add to input tag: data-validation-engine="validate[required, custom[validateMIME[image/jpeg|image/png]]]"

    var fileInput = field[0].files[0];
    var MimeFilter = new RegExp(rules[3],'i');

    if (fileInput) {
        return MimeFilter.test(fileInput.type);
    } else { return true;}
  },
"alertText": "* Wrong Mime Type."

},