From e2ea37161afb886929fbf40704b464f6aaab16ce Mon Sep 17 00:00:00 2001 From: Todd Wolfson Date: Wed, 8 Jul 2015 17:28:00 -0500 Subject: [PATCH] Additionals: Added maxsize plugin --- src/additional/maxsize.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/additional/maxsize.js diff --git a/src/additional/maxsize.js b/src/additional/maxsize.js new file mode 100644 index 000000000..148d4b609 --- /dev/null +++ b/src/additional/maxsize.js @@ -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