Skip to main content

How to validate input type file size in javascript

You can validate input type file size by getting the size of the image by comparing the specified size on which you want to validate in javascript.

  • Validate input type file size on change in javascript for single file

    <input type="file" name="file" id="file" />
    <div id="resp"></div>
    // jQuery CDN 
    <script src="" integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    var fileInput = document.getElementById('file');
    fileInput.addEventListener("change", function(event){
        let file =[0];
        let ResponseTxt = '';
        if(file.size > 1048576){
              ResponseTxt += 'File : ' + ' is greater than 1MB';
          ResponseTxt += 'File : ' + ' is a valid file';
        document.getElementById('resp').innerHTML = "";
        document.getElementById('resp').innerHTML += ResponseTxt;

  • Back to code snippet queries related javascript