Answers for "how to validate file extension in javascript"

1

how to validate file extension in javascript

//If your code above is simply trying to get the file extension of the filename then you can split the filename to an array by . and then use pop(). You can also put the valid extensions in an array and use indexOf() to check for validity. Try this:

var validExt = [ 'pdf', 'jpg', 'png' ];
var ext = this.value.split('.').pop();

if (validExt.indexOf(ext.toLowerCase()) == -1) {
    alert('Not allowed file type');
    this.value = '';
}
Posted by: Guest on December-30-2020

Code answers related to "how to validate file extension in javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language