file.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. (function($) {
  2. $.fn.bootstrapValidator.i18n.file = $.extend($.fn.bootstrapValidator.i18n.file || {}, {
  3. 'default': 'Please choose a valid file'
  4. });
  5. $.fn.bootstrapValidator.validators.file = {
  6. html5Attributes: {
  7. extension: 'extension',
  8. maxfiles: 'maxFiles',
  9. minfiles: 'minFiles',
  10. maxsize: 'maxSize',
  11. minsize: 'minSize',
  12. maxtotalsize: 'maxTotalSize',
  13. mintotalsize: 'minTotalSize',
  14. message: 'message',
  15. type: 'type'
  16. },
  17. /**
  18. * Validate upload file. Use HTML 5 API if the browser supports
  19. *
  20. * @param {BootstrapValidator} validator The validator plugin instance
  21. * @param {jQuery} $field Field element
  22. * @param {Object} options Can consist of the following keys:
  23. * - extension: The allowed extensions, separated by a comma
  24. * - maxFiles: The maximum number of files
  25. * - minFiles: The minimum number of files
  26. * - maxSize: The maximum size in bytes
  27. * - minSize: The minimum size in bytes
  28. * - maxTotalSize: The maximum size in bytes for all files
  29. * - minTotalSize: The minimum size in bytes for all files
  30. * - message: The invalid message
  31. * - type: The allowed MIME type, separated by a comma
  32. * @returns {Boolean}
  33. */
  34. validate: function(validator, $field, options) {
  35. var value = $field.val();
  36. if (value === '') {
  37. return true;
  38. }
  39. var ext,
  40. extensions = options.extension ? options.extension.toLowerCase().split(',') : null,
  41. types = options.type ? options.type.toLowerCase().split(',') : null,
  42. html5 = (window.File && window.FileList && window.FileReader);
  43. if (html5) {
  44. // Get FileList instance
  45. var files = $field.get(0).files,
  46. total = files.length,
  47. totalSize = 0;
  48. // Check the maxFiles
  49. if (options.maxFiles && total > parseInt(options.maxFiles, 10)) {
  50. return false;
  51. }
  52. // Check the minFiles
  53. if (options.minFiles && total < parseInt(options.minFiles, 10)) {
  54. return false;
  55. }
  56. for (var i = 0; i < total; i++) {
  57. totalSize += files[i].size;
  58. // Check the minSize
  59. if (options.minSize && files[i].size < parseInt(options.minSize, 10)) {
  60. return false;
  61. }
  62. // Check the maxSize
  63. if (options.maxSize && files[i].size > parseInt(options.maxSize, 10)) {
  64. return false;
  65. }
  66. // Check the maxTotalSize
  67. if (options.maxTotalSize && totalSize > parseInt(options.maxTotalSize, 10)) {
  68. return false;
  69. }
  70. // Check file extension
  71. ext = files[i].name.substr(files[i].name.lastIndexOf('.') + 1);
  72. if (extensions && $.inArray(ext.toLowerCase(), extensions) === -1) {
  73. return false;
  74. }
  75. // Check file type
  76. if (files[i].type && types && $.inArray(files[i].type.toLowerCase(), types) === -1) {
  77. return false;
  78. }
  79. }
  80. // Check the minTotalSize
  81. if (options.minTotalSize && totalSize < parseInt(options.minTotalSize, 10)) {
  82. return false;
  83. }
  84. } else {
  85. // Check file extension
  86. ext = value.substr(value.lastIndexOf('.') + 1);
  87. if (extensions && $.inArray(ext.toLowerCase(), extensions) === -1) {
  88. return false;
  89. }
  90. }
  91. return true;
  92. }
  93. };
  94. }(window.jQuery));