DEV Community


Posted on • Originally published at

How To Validate Max File Size Using Javascript

In this tutorial I will give you example of how to validate max file size using javascript, Some time we have requirement to check validation of maximum size of file before uploading in server or using javascript check validation of max file size upload. So, we will perform example of upload max file size validation in javascript.

In this example we are use on change event and check file length with file size. and display error message if file size is max given size else display success message.

  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>How To Validate Max File Size Using Javascript -</title>
  <link href="" rel="stylesheet">
  <script src=""></script>  
  <h3 style="text-align: center;margin-top:50px">How To Validate Max File Size Using Javascript -</h3>
  <div class="col-md-6 col-md-offset-5"><br>
  <input type="file" name="file"  id="filesizecheck"><br>
  <span id="error-message" class="validation-error-label"></span>
<script type="text/javascript">
      for(var i=0; i< $(this).get(0).files.length; ++i){
        var file1 = $(this).get(0).files[i].size;
          var file_size = $(this).get(0).files[i].size;
          if(file_size > 2000000){
            $('#error-message').html("File upload size is larger than 2MB");
Enter fullscreen mode Exit fullscreen mode

Read Also : How To Check RAM And CPU Usage In Laravel

And you will get output like this :

How To Validate Max File Size Using Javascript

You might also like :

Read Also : Basic Github And Git Commands

Read Also : Laravel Datatable Example Tutorial

Read Also : Import Export CSV/EXCEL File In Laravel

Top comments (0)