bootstrapValidator.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**
  2. * BootstrapValidator (https://github.com/nghuuphuoc/bootstrapvalidate)
  3. *
  4. * A jQuery plugin to validate form fields. Use with Bootstrap 3
  5. *
  6. * @author Nguyen Huu Phuoc <phuoc@huuphuoc.me>
  7. * @copyright (c) 2013 Nguyen Huu Phuoc
  8. * @license MIT
  9. */
  10. (function($) {
  11. var BootstrapValidator = function(form, options) {
  12. this.$form = $(form);
  13. this.options = $.extend({}, BootstrapValidator.DEFAULT_OPTIONS, options);
  14. this.invalidFields = {};
  15. this.xhrRequests = {};
  16. this.numPendingRequests = 0;
  17. this._init();
  18. };
  19. // The default options
  20. BootstrapValidator.DEFAULT_OPTIONS = {
  21. // The form CSS class
  22. elementClass: 'bootstrap-validator-form',
  23. // Default invalid message
  24. message: 'This value is not valid',
  25. // Map the field name with validator rules
  26. fields: null
  27. };
  28. BootstrapValidator.prototype = {
  29. constructor: BootstrapValidator,
  30. _init: function() {
  31. if (this.options.fields == null) {
  32. return;
  33. }
  34. var that = this;
  35. this.$form
  36. .addClass(this.options.elementClass)
  37. .on('submit', function(e) {
  38. that.formSubmited = true;
  39. if (that.options.fields) {
  40. for (var field in that.options.fields) {
  41. if(that.numPendingRequests > 0){
  42. that.validateField(field);
  43. }
  44. }
  45. if (!that.isValid()) {
  46. e.preventDefault();
  47. }
  48. }
  49. });
  50. for (var field in this.options.fields) {
  51. this._initField(field);
  52. }
  53. },
  54. _initField: function(field) {
  55. if (this.options.fields[field] == null || this.options.fields[field].validators == null) {
  56. return;
  57. }
  58. var $field = this.getFieldElement(field);
  59. if (null == $field) {
  60. return;
  61. }
  62. // Create a help block element for showing the error
  63. var that = this,
  64. $parent = $field.parents('.form-group'),
  65. helpBlock = $parent.find('.help-block');
  66. if (helpBlock.length == 0) {
  67. var $small = $('<small/>').addClass('help-block').appendTo($parent);
  68. $field.data('bootstrapValidator.error', $small);
  69. // Calculate the number of columns of the label/field element
  70. // Then set offset to the help block element
  71. var label, cssClasses, offset;
  72. if (label = $parent.find('label').get(0)) {
  73. cssClasses = $(label).attr('class').split(' ');
  74. for (var i = 0; i < cssClasses.length; i++) {
  75. if (cssClasses[i].substr(0, 7) == 'col-lg-') {
  76. offset = cssClasses[i].substr(7);
  77. break;
  78. }
  79. }
  80. } else {
  81. cssClasses = $parent.children().eq(0).attr('class').split(' ');
  82. for (var i = 0; i < cssClasses.length; i++) {
  83. if (cssClasses[i].substr(0, 14) == 'col-lg-offset-') {
  84. offset = cssClasses[i].substr(14);
  85. break;
  86. }
  87. }
  88. }
  89. $small.addClass('col-lg-offset-' + offset).addClass('col-lg-' + parseInt(12 - offset));
  90. } else {
  91. $field.data('bootstrapValidator.error', helpBlock.eq(0));
  92. }
  93. var type = $field.attr('type'),
  94. event = ('checkbox' == type || 'radio' == type) ? 'change' : 'keyup';
  95. $field.on(event, function() {
  96. that.validateField(field);
  97. });
  98. },
  99. getFieldElement: function(field) {
  100. var fields = this.$form.find('[name="' + field + '"]');
  101. return (fields.length == 0) ? null : $(fields[0]);
  102. },
  103. validateField: function(field) {
  104. var $field = this.getFieldElement(field);
  105. if (null == $field) {
  106. // Return if cannot find the field with given name
  107. return;
  108. }
  109. var that = this,
  110. validators = that.options.fields[field].validators;
  111. for (var validatorName in validators) {
  112. if (!$.fn.bootstrapValidator.validators[validatorName]
  113. // || (this.xhrRequests[field] && this.xhrRequests[field][validatorName])
  114. ) { // Do not perform if there is pending remote validation
  115. continue;
  116. }
  117. var isValid = $.fn.bootstrapValidator.validators[validatorName].validate(that, $field, validators[validatorName]);
  118. if (isValid === false) {
  119. that.showError($field, validatorName);
  120. break;
  121. } else if (isValid === true) {
  122. that.removeError($field);
  123. }
  124. }
  125. },
  126. showError: function($field, validatorName) {
  127. var field = $field.attr('name'),
  128. validator = this.options.fields[field].validators[validatorName],
  129. message = validator.message || this.options.message,
  130. $parent = $field.parents('.form-group');
  131. this.invalidFields[field] = true;
  132. // Add has-error class to parent element
  133. $parent.removeClass('has-success').addClass('has-error');
  134. $field.data('bootstrapValidator.error').html(message).show();
  135. },
  136. removeError: function($field) {
  137. delete this.invalidFields[$field.attr('name')];
  138. $field.parents('.form-group').removeClass('has-error').addClass('has-success');
  139. $field.data('bootstrapValidator.error').hide();
  140. },
  141. startRequest: function($field, validatorName, xhr) {
  142. var field = $field.attr('name');
  143. this.numPendingRequests++;
  144. // Abort the previous request
  145. if (!this.xhrRequests[field]) {
  146. this.xhrRequests[field] = {};
  147. }
  148. if (this.xhrRequests[field][validatorName]) {
  149. this.numPendingRequests--;
  150. this.xhrRequests[field][validatorName].abort();
  151. }
  152. this.xhrRequests[field][validatorName] = xhr;
  153. },
  154. completeRequest: function($field, validatorName, isValid) {
  155. var field = $field.attr('name');
  156. this.numPendingRequests--;
  157. if (this.numPendingRequests < 0) {
  158. this.numPendingRequests = 0;
  159. }
  160. delete this.xhrRequests[field][validatorName];
  161. },
  162. isValid: function() {
  163. if (this.numPendingRequests > 0) {
  164. return false;
  165. }
  166. for (var field in this.invalidFields) {
  167. if (this.invalidFields[field]) {
  168. return false;
  169. }
  170. }
  171. return true;
  172. }
  173. };
  174. // Plugin definition
  175. $.fn.bootstrapValidator = function(options) {
  176. return this.each(function() {
  177. var $this = $(this), data = $this.data('bootstrapValidator');
  178. if (!data) {
  179. $this.data('bootstrapValidator', (data = new BootstrapValidator(this, options)));
  180. }
  181. });
  182. };
  183. // Available validators
  184. $.fn.bootstrapValidator.validators = {};
  185. $.fn.bootstrapValidator.Constructor = BootstrapValidator;
  186. }(window.jQuery));