title: Limit file size upload in JavaScript
body: <p>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 :   </p>
  
  <pre><code>&lt;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 &gt; elem.getAttribute("data-max_size") || fname &gt; elem.getAttribute("maxlength")) {
      elem.setCustomValidity(elem.getAttribute("title"));
  } else {
  
      elem.setCustomValidity("");
  }
  </code></pre>
  
  <p>}</p>
  
  <p>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 ? </p>