Tuesday, August 2, 2022

File Size Validations

 jQuery File size validations. Arrow functions for file size validations.


    Filevalidation = () => {

        document.getElementById('size').innerHTML = '';

        let fi = document.getElementById('files');

        // Check if any file is selected.

        if (fi.files.length > 0) {

            for (const i = 0; i <= fi.files.length - 1; i++) {


                const fsize = fi.files.item(i).size;

                const file = Math.round((fsize / 1024));

                // The size of the file.

                if (file >= 4096) {

                    alert(

                        "File too Big, please select a file less than 4mb");

                    document.getElementById('files').value = '';

                } else if (file <= 1 ) {

                    alert(

                        "File too small, please select a file greater than 1KB");

                } else {

                    document.getElementById('size').innerHTML = '<b>'

                        + file + '</b> KB';

                }

            }

        }

    }


HTML:    <input type="file" name="files" id="files" multiple="multiple" onchange="Filevalidation()" />

No comments:

Post a Comment

Screen Record

 Windows Screen Record WindowsKey+ Alt + R Recording Starts.