0

I'm allowing users to upload a file, however I need to check if the file has a proper name (since using multiple '.' characters throws off the .split() method). Here's what I have so far:

//Check if filename has more than 1 period character in the whole thing. 
if(this.file.name.???){
  this.fileNameAccepted = true;
} else {
  this.fileNameAccepted = false;
}
FiringBlanks
  • 1,998
  • 4
  • 32
  • 48

1 Answers1

-1
let fileNameSplit = this.file.name.split('.');

The .split() method returns another array element for each '.' it finds. So fileNameSplit will have two elements if there are no '.' characters in the filename (there is automatically a '.' at the end of a filename before the file extension, ie hello-world.jpg, where it will always split a filename). A filename with 2 periods in the name (and 1 before the extension) will return 4 elements.

Use fileNameSplit[fileNameSplit.length - 1] to determine the last element (the file extension).

FiringBlanks
  • 1,998
  • 4
  • 32
  • 48