2

My jquery validate like this :

store: {
    rules: {
        banner: {
            required: true
        }
    },
    messages: {
        banner: 'The banner must be filled',
    },
    errorElement: 'span',
    errorPlacement: errorPlacement,
    highlight: highlight,
    unhighlight: unhighlight
},

If user not upload banner with dimension 1170 x 300 pixels there exist message like this :

Please upload an image with 1170 x 300 pixels dimension

How can I do it?

Sparky
  • 98,165
  • 25
  • 199
  • 285
moses toh
  • 12,344
  • 71
  • 243
  • 443

1 Answers1

5

You need to create your custom validation method

$.validator.addMethod('dimention', function(value, element, param) {
    if(element.files.length == 0){
        return true; <--- check here if file not added than return true for not check file dimention
    }
    var width = $(element).data('imageWidth');
    var height = $(element).data('imageHeight');
    if(width == param[0] && height == param[1]){
        return true;
    }else{
        return false;
    }
},'Please upload an image with 1170 x 300 pixels dimension');

And you need to set parameter imageWidth and imageHeight at image change like this

// files is id of your input

<input type="file" id="files" name="name" />

$('#files').change(function() {
    $('#files').removeData('imageWidth');
    $('#files').removeData('imageHeight');
    var file = this.files[0];
    var tmpImg = new Image();
    tmpImg.src=window.URL.createObjectURL( file ); 
    tmpImg.onload = function() {
        width = tmpImg.naturalWidth,
        height = tmpImg.naturalHeight;
        $('#files').data('imageWidth', width);
        $('#files').data('imageHeight', height);
    }
});

and call like this

rules: {
    banner: {
        dimention:[1170,300]
    }
}

Working fiddle link fiddle link

bipin patel
  • 2,061
  • 1
  • 13
  • 22