Browse Source

#1001: Add minFiles, maxFiles, minTotalSize, maxTotalSize options for the file validator, thanks to @morrizon

Phuoc Nguyen 11 years ago
parent
commit
29aa8661f7

+ 1 - 0
CHANGELOG.md

@@ -7,6 +7,7 @@ __New Features__
 * [#822](https://github.com/nghuuphuoc/bootstrapvalidator/pull/822): Add color validator, thanks to [@emilchristensen](https://github.com/emilchristensen)
 * [#822](https://github.com/nghuuphuoc/bootstrapvalidator/pull/822): Add color validator, thanks to [@emilchristensen](https://github.com/emilchristensen)
 * [#844](https://github.com/nghuuphuoc/bootstrapvalidator/pull/844), [#874](https://github.com/nghuuphuoc/bootstrapvalidator/pull/874): The [stringLength](http://bootstrapvalidator.com/validators/stringLength/) validator adds option to evaluate length in UTF-8 bytes, thanks to [@thx2001r](https://github.com/thx2001r)
 * [#844](https://github.com/nghuuphuoc/bootstrapvalidator/pull/844), [#874](https://github.com/nghuuphuoc/bootstrapvalidator/pull/874): The [stringLength](http://bootstrapvalidator.com/validators/stringLength/) validator adds option to evaluate length in UTF-8 bytes, thanks to [@thx2001r](https://github.com/thx2001r)
 * [#960](https://github.com/nghuuphuoc/bootstrapvalidator/issues/960): Add ```trim``` option for the [stringLength](http://bootstrapvalidator.com/validators/stringLength/) validator
 * [#960](https://github.com/nghuuphuoc/bootstrapvalidator/issues/960): Add ```trim``` option for the [stringLength](http://bootstrapvalidator.com/validators/stringLength/) validator
+* [#1001](https://github.com/nghuuphuoc/bootstrapvalidator/pull/1001): Add ```minFiles```, ```maxFiles```, ```minTotalSize```, ```maxTotalSize``` options for the [file](http://bootstrapvalidator.com/validators/file/) validator, thanks to [@morrizon](https://github.com/morrizon)
 
 
 __Improvements__
 __Improvements__
 * [#823](https://github.com/nghuuphuoc/bootstrapvalidator/issues/823): The [hexColor](http://bootstrapvalidator.com/validators/hexColor/) validator only accepts 6 hex character values when using HTML 5 ```type='color'``` attribute
 * [#823](https://github.com/nghuuphuoc/bootstrapvalidator/issues/823): The [hexColor](http://bootstrapvalidator.com/validators/hexColor/) validator only accepts 6 hex character values when using HTML 5 ```type='color'``` attribute

+ 1 - 0
CONTRIBUTORS.md

@@ -67,6 +67,7 @@ I would like to give big thanks to the following contributors:
 * [@mbezhanov](https://github.com/mbezhanov)
 * [@mbezhanov](https://github.com/mbezhanov)
 * [@mgibas](https://github.com/mgibas)
 * [@mgibas](https://github.com/mgibas)
 * [@mike1e](https://github.com/mike1e)
 * [@mike1e](https://github.com/mike1e)
+* [@morrizon](https://github.com/morrizon)
 * [@mraiur](https://github.com/mraiur)
 * [@mraiur](https://github.com/mraiur)
 * [@MrC0mm0n](https://github.com/MrC0mm0n)
 * [@MrC0mm0n](https://github.com/MrC0mm0n)
 * [@mrpollo](https://github.com/mrpollo)
 * [@mrpollo](https://github.com/mrpollo)

+ 1 - 1
dist/css/bootstrapValidator.min.css

@@ -2,7 +2,7 @@
  * BootstrapValidator (http://bootstrapvalidator.com)
  * BootstrapValidator (http://bootstrapvalidator.com)
  * The best jQuery plugin to validate form fields. Designed to use with Bootstrap 3
  * The best jQuery plugin to validate form fields. Designed to use with Bootstrap 3
  *
  *
- * @version     v0.5.3-dev, built on 2014-10-21 8:33:17 AM
+ * @version     v0.5.3-dev, built on 2014-10-21 8:50:43 AM
  * @author      https://twitter.com/nghuuphuoc
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT
  * @license     MIT

+ 32 - 20
dist/js/bootstrapValidator.js

@@ -2,7 +2,7 @@
  * BootstrapValidator (http://bootstrapvalidator.com)
  * BootstrapValidator (http://bootstrapvalidator.com)
  * The best jQuery plugin to validate form fields. Designed to use with Bootstrap 3
  * The best jQuery plugin to validate form fields. Designed to use with Bootstrap 3
  *
  *
- * @version     v0.5.3-dev, built on 2014-10-21 8:33:17 AM
+ * @version     v0.5.3-dev, built on 2014-10-21 8:50:43 AM
  * @author      https://twitter.com/nghuuphuoc
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT
  * @license     MIT
@@ -3045,8 +3045,12 @@ if (typeof jQuery === 'undefined') {
     $.fn.bootstrapValidator.validators.file = {
     $.fn.bootstrapValidator.validators.file = {
         html5Attributes: {
         html5Attributes: {
             extension: 'extension',
             extension: 'extension',
+            maxfiles: 'maxFiles',
+            minfiles: 'minFiles',
             maxsize: 'maxSize',
             maxsize: 'maxSize',
             minsize: 'minSize',
             minsize: 'minSize',
+            maxtotalsize: 'maxTotalSize',
+            mintotalsize: 'minTotalSize',
             message: 'message',
             message: 'message',
             type: 'type'
             type: 'type'
         },
         },
@@ -3058,8 +3062,12 @@ if (typeof jQuery === 'undefined') {
          * @param {jQuery} $field Field element
          * @param {jQuery} $field Field element
          * @param {Object} options Can consist of the following keys:
          * @param {Object} options Can consist of the following keys:
          * - extension: The allowed extensions, separated by a comma
          * - 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
          * - maxSize: The maximum size in bytes
-         * - minSize: the minimum 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
          * - message: The invalid message
          * - type: The allowed MIME type, separated by a comma
          * - type: The allowed MIME type, separated by a comma
          * @returns {Boolean}
          * @returns {Boolean}
@@ -3077,29 +3085,33 @@ if (typeof jQuery === 'undefined') {
 
 
             if (html5) {
             if (html5) {
                 // Get FileList instance
                 // Get FileList instance
-                var files = $field.get(0).files,
-                    total = files.length;
+                var files     = $field.get(0).files,
+                    total     = files.length,
+                    totalSize = 0;
+
+                if ((options.maxFiles && total > parseInt(options.maxFiles, 10))        // Check the maxFiles
+                    || (options.minFiles && total < parseInt(options.minFiles, 10)))    // Check the minFiles
+                {
+                    return false;
+                }
+
                 for (var i = 0; i < total; i++) {
                 for (var i = 0; i < total; i++) {
-                    // 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;
-                    }
+                    totalSize += files[i].size;
+                    ext        = files[i].name.substr(files[i].name.lastIndexOf('.') + 1);
 
 
-                    // Check file extension
-                    ext = files[i].name.substr(files[i].name.lastIndexOf('.') + 1);
-                    if (extensions && $.inArray(ext.toLowerCase(), extensions) === -1) {
+                    if ((options.minSize && files[i].size < parseInt(options.minSize, 10))                      // Check the minSize
+                        || (options.maxSize && files[i].size > parseInt(options.maxSize, 10))                   // Check the maxSize
+                        || (extensions && $.inArray(ext.toLowerCase(), extensions) === -1)                      // Check file extension
+                        || (files[i].type && types && $.inArray(files[i].type.toLowerCase(), types) === -1))    // Check file type
+                    {
                         return false;
                         return false;
                     }
                     }
+                }
 
 
-                    // Check file type
-                    if (files[i].type && types && $.inArray(files[i].type.toLowerCase(), types) === -1) {
-                        return false;
-                    }
+                if ((options.maxTotalSize && totalSize > parseInt(options.maxTotalSize, 10))        // Check the maxTotalSize
+                    || (options.minTotalSize && totalSize < parseInt(options.minTotalSize, 10)))    // Check the minTotalSize
+                {
+                    return false;
                 }
                 }
             } else {
             } else {
                 // Check file extension
                 // Check file extension

File diff suppressed because it is too large
+ 4 - 4
dist/js/bootstrapValidator.min.js


+ 15 - 35
src/js/validator/file.js

@@ -46,52 +46,32 @@
 
 
             if (html5) {
             if (html5) {
                 // Get FileList instance
                 // Get FileList instance
-                var files = $field.get(0).files,
-                    total = files.length,
+                var files     = $field.get(0).files,
+                    total     = files.length,
                     totalSize = 0;
                     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;
+                if ((options.maxFiles && total > parseInt(options.maxFiles, 10))        // Check the maxFiles
+                    || (options.minFiles && total < parseInt(options.minFiles, 10)))    // Check the minFiles
+                {
+                    return false;
                 }
                 }
 
 
                 for (var i = 0; i < total; i++) {
                 for (var i = 0; i < total; i++) {
                     totalSize += files[i].size;
                     totalSize += files[i].size;
+                    ext        = files[i].name.substr(files[i].name.lastIndexOf('.') + 1);
 
 
-                    // 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) {
+                    if ((options.minSize && files[i].size < parseInt(options.minSize, 10))                      // Check the minSize
+                        || (options.maxSize && files[i].size > parseInt(options.maxSize, 10))                   // Check the maxSize
+                        || (extensions && $.inArray(ext.toLowerCase(), extensions) === -1)                      // Check file extension
+                        || (files[i].type && types && $.inArray(files[i].type.toLowerCase(), types) === -1))    // Check file type
+                    {
                         return false;
                         return false;
                     }
                     }
                 }
                 }
 
 
-                // Check the minTotalSize
-                if (options.minTotalSize && totalSize < parseInt(options.minTotalSize, 10)) {
+                if ((options.maxTotalSize && totalSize > parseInt(options.maxTotalSize, 10))        // Check the maxTotalSize
+                    || (options.minTotalSize && totalSize < parseInt(options.minTotalSize, 10)))    // Check the minTotalSize
+                {
                     return false;
                     return false;
                 }
                 }
             } else {
             } else {