1

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');


    }
0

2 Answers 2

2

Working fiddle HERE

Code

$(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[0].size > 50000 || this.files[0].size > 50000)
    {
        //reset file upload control
       $(this).val('');
       //show an alert to the user
       alert('Allowed file size exceeded 50KB');


    }
}); });

Fore more refer Here . It usesthis.files[0].size means get file size for 1st selectd file(If multiselect is enabled , checks for 1st image) ,anyway you are not having multiselect so you access it using 0th index of files selected because you can select only 1 file in regular operation.

Sign up to request clarification or add additional context in comments.

2 Comments

What if I have 6 <input type='file'> in my form?
@GiangNguyen , no issue with multiple files .Check here => jsfiddle.net/5h29Lxv7/2 .It just makes sure for each <input type="file> It checks for 1st selected file in case of multiselect.
-1

$(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[0].size > 50000)
    {
        //reset file upload control
       $(this).val('');
       //show an alert to the user
       alert('Allowed file size exceeded 50KB');


    }
}); });
<input type="file"/>

2 Comments

I dont think its anyway different than answer i posted .
There is no need of this.files[0].size > 50000 condition twice i think

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.