I am trying to limit the size on file when user is uploading. I am new to JavaScript so any help is welcome.This is what i am trying :
<input type="file" id="input_file" name="input_file" required maxlength="40" onchange="checkFileSize(this)" data-max_size="1048576" title="bla bla"
function checkFileSize(elem) {
var fsize = elem.files[0].size;
var fname = elem.files[0].name.length;
if (fsize > elem.getAttribute("data-max_size") || fname > elem.getAttribute("maxlength")) {
elem.setCustomValidity(elem.getAttribute("title"));
} else {
elem.setCustomValidity("");
}
}
When user uploads a file bigger than 1mb or there are more thn 40 characters in file name it doesnt fire title(bla bla), nothing happens. What i am doing wrong ?
fsizeandfnamein console?