how to validate file extension in javascript

Solutions on MaxInterview for how to validate file extension in javascript by the best coders in the world

showing results for - "how to validate file extension in javascript"
Hortense
05 Aug 2020
1//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:
2
3var validExt = [ 'pdf', 'jpg', 'png' ];
4var ext = this.value.split('.').pop();
5
6if (validExt.indexOf(ext.toLowerCase()) == -1) {
7    alert('Not allowed file type');
8    this.value = '';
9}