How to validate input type file size in javascript
Created at 18-Aug-2021 ,
By samar
How to validate input type file size in javascript
In this session, we’ll try our hand at solving the "How to validate input type file size in javascript" puzzle by using the computer language.
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="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script> <script> var fileInput = document.getElementById('file'); fileInput.addEventListener("change", function(event){ let file = event.target.files[0]; let ResponseTxt = ''; if(file.size > 1048576){ ResponseTxt += 'File : '+file.name + ' is greater than 1MB'; }else{ ResponseTxt += 'File : '+file.name + ' is a valid file'; } document.getElementById('resp').innerHTML = ""; document.getElementById('resp').innerHTML += ResponseTxt; }); </script> <!-- https://codepen.io/samar1992/pen/YzVoyVy -->
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Javascript
- How to check caps lock is on in javascript
- How to check file is an image in javascript using filename
- How to set href value of anchor tag in javascript
- Prism js horizontally overflowing its container
- Javascript get only 10 characters from string
- How can I add a key/value pair to a JavaScript object
- Generate 6 digit random number in javascript
- How to switch between Dark Mode and Light Mode based on cookie in Javascript
- Get multiplication in javascript using array input
- Multidimensional array example javascript
- ReferenceError: Cannot access 'myFunction' before initialization
- Uncaught SyntaxError: Identifier 'CODE' has already been declared
- Remove property from object in javascript
- Uncaught TypeError: Illegal invocation
- Uncaught TypeError: Assignment to constant variable
- How to preview a word document in HTML using javascript
- Ezsnippets (76) challenge - javascript - youtube shorts
- Apex bar chart - fill bar color with gradient on hover
- Execute function with condition in javascript
- How to fixed bootstrap navbar at top on scroll
- How to remove duplicate values from array in javascript
- Javascript to print Fibonacci series upto 15 Observations
- How to use JavaScript to search for items in a list
- How to get query string value in javascript
- How to crop multiple images with cropper js