Image Dimension Validation with jQuery Validation

3

Even though I select an image in the correct dimensions it is giving me an error alert that I am not selecting an image in the right dimensions.

How do I return this function to not get into the error?

jQuery

$.validator.addMethod("validateimg", function(value, element, params){
    var img = new Image();
    var id  = element.id;
    img.src = window.URL.createObjectURL($('#'+id)[0].files[0]);

    img.onload  = function(){
        var w   = img.naturalWidth;
        var h   = img.naturalHeight;
        window.URL.revokeObjectURL(img.src);
        var boo = (w == params[0] && h == params[1]) ? true : false;
        return boo;
    }
});

$("#form-perfil").validate({
   ignore: [],
   errorLabelContainer      : "#error-box-content",
   wrapper                  : 'li',
   rules                    : {
       foto_perfil_admin    :{
          required          : true,
          extension         : "jpg|png",
          validateimg       : [160, 160]
       },
   },
   messages                 : {
        foto_perfil_admin   :{
        required        : 'Escolha uma foto de perfil',
        extension       : 'A foto de perfil do administrador deve ser JPG ou PNG',
        validateimg     : 'Selecione a imagem de perfil do administrador em 160 x 160',
   },
});

I think it's because the boo variable is within onLoad () .

And besides I have several image fields with the same validation and it is giving problems, conflict ...

    
asked by anonymous 29.03.2016 / 13:58

0 answers