Skip to content

Allow "accept" filetype validation to contain a blank among the accepted MIME types #1441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/additional/accept.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ $.validator.addMethod("accept", function(value, element, param) {
// Split mime on commas in case we have multiple types we can accept
var typeParam = typeof param === "string" ? param.replace(/\s/g, "").replace(/,/g, "|") : "image/*",
optionalValue = this.optional(element),
blankTypeAllowed = false,
i, file;

// Element is optional
Expand All @@ -14,11 +15,23 @@ $.validator.addMethod("accept", function(value, element, param) {
// If we are using a wildcard, make it regex friendly
typeParam = typeParam.replace(/\*/g, ".*");

// Allow checking for BLANK mimetype.
if (typeParam.match(/^\|/) || typeParam.match(/\|\|/) || typeParam.match(/\|$/)) {
blankTypeAllowed = true;
// remove the error-prone blank entry from regex before continuing.
typeParam = typeParam.replace(/^\|/, "").replace(/\|\|/g, "|").replace(/\|$/, "");
}

// Check if the element has a FileList before checking each file
if (element.files && element.files.length) {
for (i = 0; i < element.files.length; i++) {
file = element.files[i];

// Allow blank mimetype if regex contained an empty entry.
if (file.type === "" && blankTypeAllowed) {
continue; // skip the regex check
}

// Grab the mimetype from the loaded file, verify it matches
if (!file.type.match(new RegExp( ".?(" + typeParam + ")$", "i"))) {
return false;
Expand Down