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

we are a community of more than 2 million smartest coders
registration for
employee referral programs
are now open
get referred to google, amazon, flipkart and more
register now
  
pinned-register now
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}