Validating file extension on upload with Javascript

user6171080

I want to validate file extension during upload, so I tried this:

HTML

<form name="userForm" ng-submit="submitForm()" novalidate>
    <input class="upload-file" data-max-size="7048" data-min-size="2048" type="file" name="filename" ng-model="userForm.file" validfile>
</form>

focusing only on file extension validation I tried this:

JS

var extn = filename.split(".").pop();
var validFormats = ['mp3', 'wav'];
if validFormats.indexOf(extn) {
    ...
}

What is wrong with the syntax if validFormats.indexOf(extn)?

eisbehr

Just get the value and compare the last chars with the array values. indexOf will return the index if found in the array, or -1 if not. So you have to compare to it to -1. When found on the first entry in an array, the index is 0, what would be false otherwise.

$(function() {
    var validFormats = ['mp3', 'wav'];
  
    $('.upload-file').change(function(e) {
        var extn = $(this).val().split(".").pop();

        if( validFormats.indexOf(extn) == -1 ) {
            $(this).val('');
            alert('please only use one of the following filetypes: ' + validFormats.join(', '));
        }
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="userForm" ng-submit="submitForm()" novalidate>
  <input class="upload-file" data-max-size="7048" data-min-size="2048" type="file" name="filename" ng-model="userForm.file" validfile>
</form>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Cakephp validating file upload "multiple"

From Dev

Cakephp validating file upload "multiple"

From Dev

File upload in PHP: Validating that a file is gpx

From Dev

Validating file extension in AngularJs before uploading

From Dev

Upload file inside chrome extension

From Dev

Upload File with Related Extension Filter

From Dev

Upload file inside chrome extension

From Dev

Validating dimensions of an uploaded image in file upload listener in PrimeFaces

From Dev

Validating dimensions of an uploaded image in file upload listener in PrimeFaces

From Dev

Javascript file with .css extension

From Dev

Get the file extension in the JavaScript

From Dev

Javascript file with .css extension

From Dev

JavaScript not Validating

From Dev

Rest extension API to upload a file in marklogic

From Java

React Native Image Upload File Extension Error

From Dev

Upload File as a Form Data through chrome extension

From Dev

Simple file upload - how to add .jpg extension

From Dev

jQuery File Upload - validate filesize and extension

From Dev

Codeigniter Modular HMVC extension - cant upload file

From Dev

File Monitoring and Upload via Google Chrome Extension

From Dev

Get file upload name and extension in JS

From Dev

File provider extension upload - iOS11

From Dev

Upload multiple file with different extension firebase storage

From Dev

Upload file and validate file extension and file size MVC 5

From Dev

How to send data to php after validating information on a javascript file

From Dev

Required file extension for JavaScript files

From Dev

Save file with different extension javascript

From Dev

invalid file type error when upload zip file extension in opencart

From Dev

PHP file upload change file name keep extension

Related Related

  1. 1

    Cakephp validating file upload "multiple"

  2. 2

    Cakephp validating file upload "multiple"

  3. 3

    File upload in PHP: Validating that a file is gpx

  4. 4

    Validating file extension in AngularJs before uploading

  5. 5

    Upload file inside chrome extension

  6. 6

    Upload File with Related Extension Filter

  7. 7

    Upload file inside chrome extension

  8. 8

    Validating dimensions of an uploaded image in file upload listener in PrimeFaces

  9. 9

    Validating dimensions of an uploaded image in file upload listener in PrimeFaces

  10. 10

    Javascript file with .css extension

  11. 11

    Get the file extension in the JavaScript

  12. 12

    Javascript file with .css extension

  13. 13

    JavaScript not Validating

  14. 14

    Rest extension API to upload a file in marklogic

  15. 15

    React Native Image Upload File Extension Error

  16. 16

    Upload File as a Form Data through chrome extension

  17. 17

    Simple file upload - how to add .jpg extension

  18. 18

    jQuery File Upload - validate filesize and extension

  19. 19

    Codeigniter Modular HMVC extension - cant upload file

  20. 20

    File Monitoring and Upload via Google Chrome Extension

  21. 21

    Get file upload name and extension in JS

  22. 22

    File provider extension upload - iOS11

  23. 23

    Upload multiple file with different extension firebase storage

  24. 24

    Upload file and validate file extension and file size MVC 5

  25. 25

    How to send data to php after validating information on a javascript file

  26. 26

    Required file extension for JavaScript files

  27. 27

    Save file with different extension javascript

  28. 28

    invalid file type error when upload zip file extension in opencart

  29. 29

    PHP file upload change file name keep extension

HotTag

Archive