Skip to content

Instantly share code, notes, and snippets.

@whatgoodisaroad
Last active December 16, 2015 20:29
Show Gist options
  • Save whatgoodisaroad/5493025 to your computer and use it in GitHub Desktop.
Save whatgoodisaroad/5493025 to your computer and use it in GitHub Desktop.
Validity File Size Validation
// This depends on a feature in modern browser as described in:
// http://stackoverflow.com/questions/3717793/javascript-file-upload-size-validation
// Add file uploads to validity's element support:
$.validity.settings.elementSupport += ", :file";
// Create a validator for minimum file size (also requires the file):
// Using assert http://validity.thatscaptaintoyou.com/Demos/index.htm#Assert
$.fn.minFileSize = function(min) {
return this
.assert(
function(fup) { return !!fup.files.length; },
"You should select a file"
)
.assert(
function(fup) { return fup.files[0].size >= min; },
"The file is too small"
);
};
// Now, in a validation session, can write...
var min = 10000;
$("#myFileUpload:file").minFileSize(min);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment