Skip to content

Instantly share code, notes, and snippets.

@crazyrohila
Created November 13, 2014 20:01
Show Gist options
  • Save crazyrohila/30ff09e445f798b16097 to your computer and use it in GitHub Desktop.
Save crazyrohila/30ff09e445f798b16097 to your computer and use it in GitHub Desktop.
has File Extension
/**
* Helper function to check file extension for <input type="file"> element.
* @param Array exts An array containing extension to check. eg. ['.png', '.zip'].
* @return Boolean returns ture only if all files are matched given extension otherwise returns false.
*/
$.fn.hasExtension = function(exts) {
var file_list = this[0].files;
for (var i = 0, file; file = file_list[i]; i++) {
var fileName = file.name;
var result = new RegExp('(' + exts.join('|').replace(/\./g, '\\.') + ')$').test(fileName);
if (!result) {
alert('Error: Invalid file extension: ' + fileName);
return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment