Validating Image Dimensions Using Jquery Plugin
I have a form where validation occurs in jQuery's validation plugin. My form contains two elements, an input type=file and a submit button. The user will select an image, and if th
Solution 1:
You can load the image file into an <img>
element, and then get the width of that element.
First add a container element to your HTML to hold the <img>
element:
<form class="some_form" enctype="multipart/form-data" method="post" action="">
<input type="file" name="photo"id="photoInput" />
<input type="submit">
</form>
<div id="imgContainer"></div>
Next, define a custom Validator method:
$.validator.addMethod('minImageWidth', function(value, element, minWidth) {
return ($(element).data('imageWidth') || 0) > minWidth;
}, function(minWidth, element) {
var imageWidth = $(element).data('imageWidth');
return (imageWidth)
? ("Your image's width must be greater than " + minWidth + "px")
: "Selected file is not an image.";
});
Notes:
- As you can see, the method expects that it can get the image width by calling
.data('imageWidth')
on the file input element. We will be setting that value in code shown below. - Also, the method assumes that when
.data('imageWidth')
returnsundefined
, the selected file is not an image.
You can now use the minImageWidth
method like this:
varvalidator=$('.some_form').validate({rules: {
photo: {
required:true,
minImageWidth:500
}
},messages: {
photo: {
required:"You must insert an image"
},
}
});
Finally, add a change-event handler for the file input that creates the <img>
element and adds it to the container. It will also set the .data('imageWidth')
value on the file input element.
var$submitBtn = $('.some_form').find('input:submit'),
$photoInput = $('#photoInput'),
$imgContainer = $('#imgContainer');
$('#photoInput').change(function() {
$photoInput.removeData('imageWidth');
$imgContainer.hide().empty();
var file = this.files[0];
if (file.type.match(/image\/.*/)) {
$submitBtn.attr('disabled', true);
var reader = new FileReader();
reader.onload = function() {
var$img = $('<img />').attr({ src: reader.result });
$img.on('load', function() {
$imgContainer.append($img).show();
var imageWidth = $img.width();
$photoInput.data('imageWidth', imageWidth);
if (imageWidth < 500) {
$imgContainer.hide();
} else {
$img.css({ width: '400px', height: '200px' });
}
$submitBtn.attr('disabled', false);
validator.element($photoInput);
});
}
reader.readAsDataURL(file);
} else {
validator.element($photoInput);
}
});
Notes:
- When the image is loaded, its width is placed as a
.data('imageWidth')
value on the file input element. - While the image is loading, the submit button is disabled so the user cannot submit the form.
- The validation is performed immediately after the image is loaded. That is, the user does not have to click the submit button in order to for the error message to show up. This is accomplished by the call to
validator.element()
. - You cannot get the width of the
<img>
element until it is added to the DOM. It must also be visible, but the code above shows how it can be hidden right after if desired.
References:
Post a Comment for "Validating Image Dimensions Using Jquery Plugin"