title: Check file size before upload
body: <p>I am using this javascript that I got from here, and it works perfectly for what I need it to.</p>
  
  <pre><code>var _validFileExtensions = [".jpg", ".jpeg"]; 
  
  function File_Validator(theForm){
      var arrInputs = theForm.getElementsByTagName("input"); 
      for (var i = 0; i &lt; arrInputs.length; i++) { 
      var oInput = arrInputs[i]; 
      if (oInput.type == "file") { 
          var sFileName = oInput.value; 
          var blnValid = false; 
              for (var j = 0; j &lt; _validFileExtensions.length; j++) { 
                  var sCurExtension = _validFileExtensions[j]; 
                  if (sFileName.substr(sFileName.length - sCurExtension.length, sCurExtension.length).toLowerCase() == sCurExtension.toLowerCase()) { 
                      blnValid = true; 
                      break; 
                      } 
                  } 
  
                  if (!blnValid) { 
                      alert("Invalid image file type!"); 
                      return false; 
                  } 
          } 
      } 
  
  return true; 
  } 
  </code></pre>
  
  <p>Now, I was wondering if, in addition, I could check the file size and fail if the file is bigger than 500kb -> all before pressing the submit/upload button?</p>
  
  <p>EDIT</p>
  
  <p>After looking into what PHPMyCoder suggested, I end up solving by using this javascript code:</p>
  
  <pre><code>&lt;script language='JavaScript'&gt;
  function checkFileSize(inputFile) {
  var max =  3 * 512 * 512; // 786MB
  
  if (inputFile.files &amp;&amp; inputFile.files[0].size &gt; max) {
      alert("File too large."); // Do your thing to handle the error.
      inputFile.value = null; // Clear the field.
     }
  }
  &lt;/script&gt;
  </code></pre>
  
  <p>This checks the file size, and alert the user before submitting the form.</p>