Skip to content

Additionals: Added maxsize plugin #1512

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
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
30 changes: 30 additions & 0 deletions src/additional/maxsize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Require a file at/under a given size
// Usage: `{rules: {file: {maxsize: 5 * 1024 * 1024 /* 5MB */}}}`
$.validator.addMethod("maxsize", function(value, element, param) {
// If the value is optional, return it
var maxSize = param,
optionalValue = this.optional(element),
i, len, file;
if (optionalValue) {
return optionalValue;
}

// If the element is a file, then verify the size is at most what we expect
if ($(element).attr("type") === "file") {
// If we are in a browser that supports FileList, then check the files
if (element.files && element.files.length) {
i = 0;
len = element.files.length;
for (; i < len; i++) {
file = element.files[i];
// `file.size` is size in bytes
if (file.size > maxSize) {
return false;
}
}
}
}

// Return success (either we don't have access to files due to older browser or files looked good)
return true;
}, $.validator.format("Please submit a file that is under the maximum size."));
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy