
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="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
Bootstrap
Cmd
CodeIgniter
Css
Drupal
Git
Html
Javascript
Jquery
Laravel
Nodejs
Php
Python
Sql
Typescript
Wordpress
Random Code Snippet Queries: Javascript
- Uncaught TypeError: Assignment to constant variable
- How to get data from localstorage in javascript
- Copy one array to another in javascript
- Multidimensional array example javascript
- How to echo array in Javascript
- How to get file extension using javascript/jquery
- How to check file is an image in javascript using filename
- How to remove duplicate values from array in javascript
- Check if value exists in array javascript
- Remove property from object in javascript
- Input type file styling
- How to use JavaScript to search for items in a list
- How to preview video before uploading in javascript
- How to pass array as arguments to function in javascript
- Cropper js with multiple images
- How to fixed bootstrap navbar at top on scroll
- How to get the length of a number in JavaScript
- Uncaught TypeError: Illegal invocation
- Trim the leading and trailing comma from string in Javascript
- Uncaught SyntaxError: Identifier 'CODE' has already been declared
- How to add a property to object with condition in javascript
- How to open modal after redirect
- Concat variable with string in javascript
- How to check caps lock is on in javascript
- Uncaught TypeError: Cannot read property 'addEventListener' of undefined