HTML
<input type="file" name="poster" accept="image/*">
$(document).ready(function() {
$('input[name=poster]').bind('change', function() {
var a = (this.files[0].size);
var iSize = (a / 1024);
iSize = (Math.round(iSize * 100) / 100)
alert(iSize + "kb");
if (iSize > 2000) {
alert('Maximum file upload size is 2MB');
return false;
}else{
return true;
}
});
});
Another way without alertbox
<script type="text/javascript">
function validate() {
$("#file_error").html("");
$(".demoInputBox").css("border-color", "#F0F0F0");
var file_size = $('#file')[0].files[0].size;
if (file_size > 2097152) {
$("#file_error").html("File size is greater than 2MB");
$(".demoInputBox").css("border-color", "#FF0000");
return false;
}
return true;
}
</script>
Universal dynamic validation
<input type="file" placeholder="" name="poster" onchange="validate_my_file(this,2000)" accept="image/*">
<script>
function validate_my_file(t, max_file_size) {
if (!max_file_size) {
max_file_size = 2000;
}
a = t.files[0].size;
var iSize = (a / 1024);
iSize = (Math.round(iSize * 100) / 100)
//alert( iSize + "kb");
if (iSize > max_file_size) {
alert('Maximum file upload size is '+max_file_size+'KB');
return false;
} else {
return true;
}
}
</script>
<input type="file" name="poster" accept="image/*">
$(document).ready(function() {
$('input[name=poster]').bind('change', function() {
var a = (this.files[0].size);
var iSize = (a / 1024);
iSize = (Math.round(iSize * 100) / 100)
alert(iSize + "kb");
if (iSize > 2000) {
alert('Maximum file upload size is 2MB');
return false;
}else{
return true;
}
});
});
Another way without alertbox
<script type="text/javascript">
function validate() {
$("#file_error").html("");
$(".demoInputBox").css("border-color", "#F0F0F0");
var file_size = $('#file')[0].files[0].size;
if (file_size > 2097152) {
$("#file_error").html("File size is greater than 2MB");
$(".demoInputBox").css("border-color", "#FF0000");
return false;
}
return true;
}
</script>
Universal dynamic validation
<input type="file" placeholder="" name="poster" onchange="validate_my_file(this,2000)" accept="image/*">
<script>
function validate_my_file(t, max_file_size) {
if (!max_file_size) {
max_file_size = 2000;
}
a = t.files[0].size;
var iSize = (a / 1024);
iSize = (Math.round(iSize * 100) / 100)
//alert( iSize + "kb");
if (iSize > max_file_size) {
alert('Maximum file upload size is '+max_file_size+'KB');
return false;
} else {
return true;
}
}
</script>
0 comments:
Post a Comment