I got this jquery file type validation on the internet. It works great. I later added some code into it to validate file's size as well. But it did not raise alert box when my file size is greater than 50KB. I just learn jquery so I am not sure what i added is correct or not. Please help.
Here is JSFIDDLE
Here is the code:
<script>
$(document).ready(function () {
$('input[type=file]').change(function () {
var val = $(this).val().toLowerCase();
var regex = new RegExp("(.*?)\.(jpg|jpeg|gif)$");
if(!(regex.test(val))) {
$(this).val('');
alert('Unsupported file');
}
if ($(this).files.size > 50000 || $(this).files.fileSize > 50000)
{
//reset file upload control
$(this).val('');
//show an alert to the user
alert('Allowed file size exceeded 50KB');
}
}); });
</script>
This is what i have added into the existing code:
if ($(this).files.size > 50000 || $(this).files.fileSize > 50000)
{
//reset file upload control
$(this).val('');
//show an alert to the user
alert('Allowed file size exceeded 50KB');
}