0

I make a form validation with javascript for field image upload but i stuck on point of how to validate image size(width and height) through javascipt for only 600 width and 300 height image is post by form .

So guys if you have any solution are demo code share with mi .

Thanks in advance !

rahul tripathi
  • 321
  • 2
  • 7
  • 18

1 Answers1

0

You can use following function in javascript side to check whether image has desired resolution or not.

If it has correct resolution than proceed with form submission otherwise show error message.

var _URL = window.URL || window.webkitURL;

$("#file").change(function(e) {
    var file, img;

    if ((file = this.files[0])) {
        img = new Image(); // create a Image object which contain height & width property of image
        img.onload = function() {
            alert(this.width + " " + this.height);
        };
        img.onerror = function() {
            alert( "not a valid file: " + file.type);
        };
        img.src = _URL.createObjectURL(file);
    }
});
Prateek
  • 4,220
  • 1
  • 17
  • 22