| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- (function($) {
- $.fn.bootstrapValidator.i18n.file = $.extend($.fn.bootstrapValidator.i18n.file || {}, {
- 'default': 'Please choose a valid file'
- });
- $.fn.bootstrapValidator.validators.file = {
- html5Attributes: {
- extension: 'extension',
- maxfiles: 'maxFiles',
- minfiles: 'minFiles',
- maxsize: 'maxSize',
- minsize: 'minSize',
- maxtotalsize: 'maxTotalSize',
- mintotalsize: 'minTotalSize',
- message: 'message',
- type: 'type'
- },
- /**
- * Validate upload file. Use HTML 5 API if the browser supports
- *
- * @param {BootstrapValidator} validator The validator plugin instance
- * @param {jQuery} $field Field element
- * @param {Object} options Can consist of the following keys:
- * - extension: The allowed extensions, separated by a comma
- * - maxFiles: The maximum number of files
- * - minFiles: The minimum number of files
- * - maxSize: The maximum size in bytes
- * - minSize: The minimum size in bytes
- * - maxTotalSize: The maximum size in bytes for all files
- * - minTotalSize: The minimum size in bytes for all files
- * - message: The invalid message
- * - type: The allowed MIME type, separated by a comma
- * @returns {Boolean}
- */
- validate: function(validator, $field, options) {
- var value = $field.val();
- if (value === '') {
- return true;
- }
- var ext,
- extensions = options.extension ? options.extension.toLowerCase().split(',') : null,
- types = options.type ? options.type.toLowerCase().split(',') : null,
- html5 = (window.File && window.FileList && window.FileReader);
- if (html5) {
- // Get FileList instance
- var files = $field.get(0).files,
- total = files.length,
- totalSize = 0;
- // Check the maxFiles
- if (options.maxFiles && total > parseInt(options.maxFiles, 10)) {
- return false;
- }
- // Check the minFiles
- if (options.minFiles && total < parseInt(options.minFiles, 10)) {
- return false;
- }
- for (var i = 0; i < total; i++) {
- totalSize += files[i].size;
- // Check the minSize
- if (options.minSize && files[i].size < parseInt(options.minSize, 10)) {
- return false;
- }
- // Check the maxSize
- if (options.maxSize && files[i].size > parseInt(options.maxSize, 10)) {
- return false;
- }
- // Check the maxTotalSize
- if (options.maxTotalSize && totalSize > parseInt(options.maxTotalSize, 10)) {
- return false;
- }
- // Check file extension
- ext = files[i].name.substr(files[i].name.lastIndexOf('.') + 1);
- if (extensions && $.inArray(ext.toLowerCase(), extensions) === -1) {
- return false;
- }
- // Check file type
- if (files[i].type && types && $.inArray(files[i].type.toLowerCase(), types) === -1) {
- return false;
- }
- }
- // Check the minTotalSize
- if (options.minTotalSize && totalSize < parseInt(options.minTotalSize, 10)) {
- return false;
- }
- } else {
- // Check file extension
- ext = value.substr(value.lastIndexOf('.') + 1);
- if (extensions && $.inArray(ext.toLowerCase(), extensions) === -1) {
- return false;
- }
- }
- return true;
- }
- };
- }(window.jQuery));
|