Created
November 13, 2014 20:01
-
-
Save crazyrohila/30ff09e445f798b16097 to your computer and use it in GitHub Desktop.
has File Extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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