1

I have a code but I need a javascript validation that checks maximum upload file size like check if the uploaded file is increased 1-MB he show error file is increased chosse less then 1MB file ...

I have this code how use maximum file size regular expression and whar code I use in this code that check maximum size validation.

<form action="" method="post">
 <script type="text/javascript">
   function ValidateExtension() {
     var allowedFiles = [".doc", ".docx", ".pdf"];
     var fileUpload = document.getElementById("fileUpload");
     var lblError = document.getElementById("lblError");
     var regex = new RegExp("([a-zA-Z0-9\s_\\.\-:])+(" + allowedFiles.join('|') + ")$");
     if (!regex.test(fileUpload.value.toLowerCase())) {
       lblError.innerHTML = "Please upload files having extensions: <b>" + allowedFiles.join(', ') + "</b> only.";
       return false;
     }
     lblError.innerHTML = "";
     return true;
   }
 </script>
<input id="fileUpload" type="file" /> 
<br /> 
<span id="lblError" style="color: red;"></span> 
<br /> 
<input type="submit" id="btnUpload" value="Upload" onclick="return ValidateExtension()" /> 
</form>
3
  • <form action="" method="post"> <script type="text/javascript"> function ValidateExtension() { var allowedFiles = [".doc", ".docx", ".pdf"]; var fileUpload = document.getElementById("fileUpload"); var lblError = document.getElementById("lblError"); var regex = new RegExp("([a-zA-Z0-9\s_\\.\-:])+(" + allowedFiles.join('|') + ")$"); if (!regex.test(fileUpload.value.toLowerCase())) { lblError.innerHTML = "Please upload files having extensions: <b>" + allowedFiles.join(', ') + "</b> only."; return false;} lblError.innerHTML = ""; return true;} </script> Commented Nov 8, 2015 at 19:45
  • <input id="fileUpload" type="file" /> <br /> <span id="lblError" style="color: red;"></span> <br /> <input type="submit" id="btnUpload" value="Upload" onclick="return ValidateExtension()" /> </form> Commented Nov 8, 2015 at 19:46
  • This code i use how i done javascript maximum size validation in this code. Commented Nov 8, 2015 at 19:46

1 Answer 1

8

This should get you started.

function validate(el) {
  var maxfilesize = 1024 * 1024,  // 1 Mb
      filesize    = el.files[0].size,
      warningel   = document.getElementById( 'lbError' );

  if ( filesize > maxfilesize )
  {
    warningel.innerHTML = "File too large: " + filesize + ". Maximum size: " + maxfilesize;
    return false;
  }
  else
  {
    warningel.innerHTML = '';
    return true;
  }   
}
.warning { font-style: italic; }
<form enctype="multipart/form-data" method="POST">
  <input type='file' name='f' onchange='validate(this)'>
  <div id='lbError' class='warning'></div>
  <input type='submit' onsubmit='return validate()'/>
</form>

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

3 Comments

Thnx but me not need this code i write code above in commeent how i check javascript validation that chaeck file size if file size is greater file is not uploaded
There was, and is, code in there that you need: the expression to access the file size. I've updated the answer to be more inline with your code; all that's left for you to do is to merge that with your code.
OK Thank u bro so much

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.