JavaScript:文件大小验证

时间:2016-09-06 09:04:17

标签: javascript file validation

我正在尝试在用户选择要上传的文件时添加文件验证。文件类型验证工作正常,但我不清楚为什么文件大小不起作用。有没有人有任何想法?

$('input[type=file]').change(function () {
var val = $(this).val().toLowerCase();
var regex = new RegExp("(.*?)\.(zip|bmp|jpg|jpeg|tif|pdf|cdr|gbr|gtl|dxf|dwg)$");
    if(!(regex.test(val))) {
       $(this).val('');
       $("#fileError").html("<span class='flash'>You are trying to upload an file format we do not accept, please refer to our <a target='_blank' href='http://uk-cpi.com/inkjet-flex/guides/design-guide'>Design Guidelines ➜</a> and upload an accepted file format.<br></span>");
    }

    if(val.size > 2097152) {
         $("#fileError").html("<span class='flash'>File size must under 2mb!</span>");
    }

});

0 个答案:

没有答案