Browse Source

#77: Add file validator

nghuuphuoc 11 years ago
parent
commit
62a09c69ff
4 changed files with 133 additions and 2 deletions
  1. 1 0
      CHANGELOG.md
  2. 65 0
      dist/js/bootstrapValidator.js
  3. 2 2
      dist/js/bootstrapValidator.min.js
  4. 65 0
      src/js/validator/file.js

+ 1 - 0
CHANGELOG.md

@@ -2,6 +2,7 @@
 
 ## v0.4.3 (not released yet)
 
+* [#77](https://github.com/nghuuphuoc/bootstrapvalidator/issues/77): Add ```file``` validator
 * [#198](https://github.com/nghuuphuoc/bootstrapvalidator/pull/198): Add Canadian Postal Code support for the [```zipCode``` validator](http://bootstrapvalidator.com/validators/zip-code/), thanks to [@Francismori7](https://github.com/Francismori7)
 * [#202](https://github.com/nghuuphuoc/bootstrapvalidator/issues/202): Activate tab containing the first invalid field
 * [#205](https://github.com/nghuuphuoc/bootstrapvalidator/issues/205): Plugin method invocation

+ 65 - 0
dist/js/bootstrapValidator.js

@@ -1353,6 +1353,71 @@
     }
 }(window.jQuery));
 ;(function($) {
+    $.fn.bootstrapValidator.validators.file = {
+        html5Attributes: {
+            extension: 'extension',
+            maxsize: 'maxSize',
+            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
+         * - maxSize: The maximum size in bytes
+         * - 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.split(',') : null,
+                types      = options.type      ? options.type.split(',')      : null,
+                html5      = (window.File && window.FileList && window.FileReader);
+
+            if (html5) {
+                // Get FileList instance
+                var files = $field.get(0).files,
+                    total = files.length;
+                for (var i = 0; i < total; i++) {
+                    // Check file size
+                    if (options.maxSize && files[i].size > parseInt(options.maxSize)) {
+                        return false;
+                    }
+
+                    // Check file extension
+                    ext = files[i].name.substr(files[i].name.lastIndexOf('.') + 1);
+                    if (extensions && extensions.indexOf(ext) == -1) {
+                        return false;
+                    }
+
+                    // Check file type
+                    if (types && types.indexOf(files[i].type) == -1) {
+                        return false;
+                    }
+                }
+            } else {
+                // Check file extension
+                ext = value.substr(value.lastIndexOf('.') + 1);
+                if (extensions && extensions.indexOf(ext) == -1) {
+                    return false;
+                }
+            }
+
+            return true;
+        }
+    };
+}(window.jQuery));
+;(function($) {
     $.fn.bootstrapValidator.validators.greaterThan = {
         html5Attributes: {
             message: 'message',

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


+ 65 - 0
src/js/validator/file.js

@@ -0,0 +1,65 @@
+(function($) {
+    $.fn.bootstrapValidator.validators.file = {
+        html5Attributes: {
+            extension: 'extension',
+            maxsize: 'maxSize',
+            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
+         * - maxSize: The maximum size in bytes
+         * - 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.split(',') : null,
+                types      = options.type      ? options.type.split(',')      : null,
+                html5      = (window.File && window.FileList && window.FileReader);
+
+            if (html5) {
+                // Get FileList instance
+                var files = $field.get(0).files,
+                    total = files.length;
+                for (var i = 0; i < total; i++) {
+                    // Check file size
+                    if (options.maxSize && files[i].size > parseInt(options.maxSize)) {
+                        return false;
+                    }
+
+                    // Check file extension
+                    ext = files[i].name.substr(files[i].name.lastIndexOf('.') + 1);
+                    if (extensions && extensions.indexOf(ext) == -1) {
+                        return false;
+                    }
+
+                    // Check file type
+                    if (types && types.indexOf(files[i].type) == -1) {
+                        return false;
+                    }
+                }
+            } else {
+                // Check file extension
+                ext = value.substr(value.lastIndexOf('.') + 1);
+                if (extensions && extensions.indexOf(ext) == -1) {
+                    return false;
+                }
+            }
+
+            return true;
+        }
+    };
+}(window.jQuery));