This is very easy to understand and customize file upload extension validation and size validation with jquery code..... In this code swal() is a function which is sweet alerts function i have used to display alert messages..
You can easy integrate Sweet Alerts click here to get this
<input type="file" class="apply-image-validation" />
<script>
$(document).on('change', '.apply-image-validation', function(e) {
e.preventDefault();
var t = $(this);
var max_file_upload_size = 1000000; //1 MB
var allowed_extensions = ["jpg", "png", "gif", "jpeg"];
var ext = this.files[0].name.split('.').pop();
if (allowed_extensions.indexOf(ext) === -1) {
$(this).val("");
alert("Please upload jpg or png or gif images only");
//swal("warning", "Please upload jpg or png or gif images only", "warning");
return false;
}
if (max_file_upload_size < this.files[0].size) {
$(this).val("");
alert("Allowed file size exceeded. (Max. 1 MB)");
//swal("Alert", "Allowed file size exceeded. (Max. 1 MB)", "warning");
return false;
}
});
</script>
You can easy integrate Sweet Alerts click here to get this
<input type="file" class="apply-image-validation" />
<script>
$(document).on('change', '.apply-image-validation', function(e) {
e.preventDefault();
var t = $(this);
var max_file_upload_size = 1000000; //1 MB
var allowed_extensions = ["jpg", "png", "gif", "jpeg"];
var ext = this.files[0].name.split('.').pop();
if (allowed_extensions.indexOf(ext) === -1) {
$(this).val("");
alert("Please upload jpg or png or gif images only");
//swal("warning", "Please upload jpg or png or gif images only", "warning");
return false;
}
if (max_file_upload_size < this.files[0].size) {
$(this).val("");
alert("Allowed file size exceeded. (Max. 1 MB)");
//swal("Alert", "Allowed file size exceeded. (Max. 1 MB)", "warning");
return false;
}
});
</script>
0 comments:
Post a Comment