Click here to Skip to main content
15,889,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Please let me know if there is any solution to validate the image dimensions before uploading it in javascript.

What I have tried:

I have tried the below code in javascript which returns the error message correctly but the form is getting submitting despite the image dimensions are not valid.

$(function () {
           $("#btnSubmit").bind("click", function () {
               //Get reference of FileUpload.
               var fileUpload = $("#fuLogo")[0];
               //event.preventDefault();
               if ((fileUpload.value != null && fileUpload.value != "") ||(document.getElementById("LogoDiv").style.display==="none")) {
                   //Check whether the file is valid Image.
                   var regex = /^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.jpg|.JPG|.jpeg|.JPEG|.bmp|.BMP|.png|.PNG|.ico|.ICO|.gif|.GIF)$/;
                   if (regex.test(fileUpload.value.toLowerCase())) {
                       //Check whether HTML5 is supported.
                       if (typeof (fileUpload.files) != "undefined") {
                           //Initiate the FileReader object.
                           var reader = new FileReader();
                           //Read the contents of Image File.
                           reader.readAsDataURL(fileUpload.files[0]);
                           reader.onload = function (e) {
                               //Initiate the JavaScript Image object.
                               var image = new Image();
                               //Set the Base64 string return from FileReader as source.
                               image.src = e.target.result;
                               image.onload = function () {
                                   //Determine the Height and Width.
                                   var height = this.height;
                                   var width = this.width;
                                   if (height > 150 || width > 1500) {
                                       //alert("Please upload image with valid dimensions.");
                                       //event.preventDefault();
                                       document.getElementById("lblLogoMsg").innerHTML = "Please upload image with valid dimensions.";
                                       //event.preventDefault();
                                       return false;
                                   }
                               };
                           }
                       }
                   }
               }
           });
       });
Posted
Updated 20-Sep-18 22:28pm
v2

1 solution

It looks like you are reading the dimensions when you submit the form, so when you "return false" if the dimensions are wrong it's the "onload" event you are cancelling, not the form submit.

If you look at this way of doing it;

javascript - Is it possible to check dimensions of image before uploading? - Stack Overflow[^]

the submit event is cancelled, and the onload event then does the form submission once the dimensions have been checked.

Google for "html5 check image dimensions before upload" and you'll find other examples too.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900