get file extension of uploaded file javascript code example

Example: 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 = '';
}