Here is my jQuery bit that validates whether image uploaded is valid or not on getting an error or success an alert box is shown
Need solution for:
- message instead of an alert box when success or error function is called
- the file should be discarded if there is an error (in my case it stays)
jQuery bit:
(function($) {
$.fn.checkFileType = function(options) {
var defaults = {
allowedExtensions: [],
success: function() {},
error: function() {}
};
options = $.extend(defaults, options);
return this.each(function() {
$(this).on('change', function() {
var value = $(this).val(),
file = value.toLowerCase(),
extension = file.substring(file.lastIndexOf('.') + 1);
if ($.inArray(extension, options.allowedExtensions) == -1) {
options.error();
$(this).focus();
} else {
options.success();
}
});
});
};
})(jQuery);
function validate() {
$('#filen').checkFileType({
allowedExtensions: ['jpg', 'jpeg', 'png', 'bmp'],
success: function() {
$("#filen").text("Valid image").show();
},
error: function() {
/*messages: {
filen: "Please insert valid image file with following extensions .jpg .jpeg .png .bmp"
},*/
alert('Error: Please insert valid image file with following extensions .jpg .jpeg .png .bmp');
reload("index134.html");
}
});
html bit:-
<tr>
<td><label>Insert File</label></td>
<td><input type="file" name="filen" id ="filen"/></td>
</tr>