
Skip to main content
How to check file is an image in javascript using filename
You can check if the file is an image using the filename in javascript. This function helps you get the file extension and return true or false if the file extension does not exist in the provided file extensions array.
-
Check the file extension in javascript using filename and return true if the file is an image
<script> function getFileExtension(fileName){ var fileExtension; fileExtension = fileName.replace(/^.*\./, ''); return fileExtension; } function isIMage(fileName){ var fileExt = getFileExtension(fileName); var imagesExtension = ["png", "jpg", "jpeg"]; if(imagesExtension.indexOf(fileExt) !== -1){ return true; } else{ return false; } } console.log(isIMage('filename.jpg')); </script>
You have to pass the filename to function. This function first checks the file extension and after that it returns true and false on the basis of the provided file extension array.
Bootstrap
Cmd
CodeIgniter
Css
Drupal
Git
Html
Javascript
Jquery
Laravel
Nodejs
Php
Python
Sql
Typescript
Wordpress
Random Code Snippet Queries: Javascript
- How to remove duplicate values from array in javascript
- How to Set data to localstorage in javascript
- Input type file styling
- Get multiplication in javascript using array input
- Get hostname from URL in javascript
- Prism js horizontally overflowing its container
- Cropper js with multiple images
- How to push string in an array in Javascript
- How to make entire Div clickable in javascript
- Deault order of record in datatable
- How to fixed bootstrap navbar at top on scroll
- How to preview a word document in HTML using javascript
- Uncaught TypeError: Assignment to constant variable
- How to open modal after redirect
- How to use JavaScript to search for items in a list
- Uncaught SyntaxError: Identifier 'CODE' has already been declared
- Trim the leading and trailing comma from string in Javascript
- JavaScript test() Method
- Javascript get data-id attribute value
- How to crop multiple images with cropper js
- ReferenceError: Cannot access 'myFunction' before initialization
- Check if value exists in array javascript
- How to get query string value in javascript
- Uncaught TypeError: $(...).summernote is not a function
- Remove hostname from URL in javascript