Skip to content

Instantly share code, notes, and snippets.

@niczak
Last active April 12, 2021 08:22
Show Gist options
  • Save niczak/5470218 to your computer and use it in GitHub Desktop.
Save niczak/5470218 to your computer and use it in GitHub Desktop.
Check file extension w/ JavaScript
function checkFile(file) {
var extension = file.substr((file.lastIndexOf('.') +1));
if (!/(pdf|zip|doc)$/ig.test(extension)) {
alert("Invalid file type: "+extension+". Please use DOC, PDF or Zip.");
$("#file").val("");
}
}
@huuluan97
Copy link

Thanks you.

@marcellobarile
Copy link

does this function do not support formats like ".tar.gz" on purpose? If not, be aware of those uncovered case.

@hoseinsajadi20
Copy link

very good, thank you for your code

@BobbyRuby
Copy link

:) nice thanks

@Windowsz
Copy link

Windowsz commented Jun 8, 2020

thankyou :> ♥

@mmhashemi
Copy link

mmhashemi commented Apr 12, 2021

thank you, but, I can set dynamic extension in this function? because you used regular expressions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment