I want to validate my input upload image with only allow image with size less than 2mb with jQuery. I find around but mostly guides are about validating dimension (width and height of image) Here's my code:
html:
<input type="file" name="logo" id="logo" accept="image/*">
<p class="error_line" style="display: none">Image Only</p>
<input class="btn btn-primary submit-btn" style="margin:10px 0; width: auto" type="submit" value="Create">
Javascript:
$(document).ready(function () {
var _URL = window.URL || window.webkitURL;
$("#logo").change(function(e) {
var file, img;
if ((file = this.files[0])) {
img = new Image();
img.onload = function() {
$('.submit-btn').prop('disabled', false);
$(".error_line").fadeOut();
};
img.onerror = function() {
$('.submit-btn').prop('disabled', true);
$(".error_line").fadeIn();
};
img.src = _URL.createObjectURL(file);
}
});
});