foundation.abide.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. ;(function ($, window, document, undefined) {
  2. 'use strict';
  3. Foundation.libs.abide = {
  4. name : 'abide',
  5. version : '5.3.3',
  6. settings : {
  7. live_validate : true,
  8. focus_on_invalid : true,
  9. error_labels: true, // labels with a for="inputId" will recieve an `error` class
  10. timeout : 1000,
  11. patterns : {
  12. alpha: /^[a-zA-Z]+$/,
  13. alpha_numeric : /^[a-zA-Z0-9]+$/,
  14. integer: /^[-+]?\d+$/,
  15. number: /^[-+]?\d*(?:[\.\,]\d+)?$/,
  16. // amex, visa, diners
  17. card : /^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$/,
  18. cvv : /^([0-9]){3,4}$/,
  19. // http://www.whatwg.org/specs/web-apps/current-work/multipage/states-of-the-type-attribute.html#valid-e-mail-address
  20. email : /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/,
  21. url: /^(https?|ftp|file|ssh):\/\/(((([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)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\?)*)?$/,
  22. // abc.de
  23. domain: /^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$/,
  24. datetime: /^([0-2][0-9]{3})\-([0-1][0-9])\-([0-3][0-9])T([0-5][0-9])\:([0-5][0-9])\:([0-5][0-9])(Z|([\-\+]([0-1][0-9])\:00))$/,
  25. // YYYY-MM-DD
  26. date: /(?:19|20)[0-9]{2}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9])|(?:(?!02)(?:0[1-9]|1[0-2])-(?:30))|(?:(?:0[13578]|1[02])-31))$/,
  27. // HH:MM:SS
  28. time : /^(0[0-9]|1[0-9]|2[0-3])(:[0-5][0-9]){2}$/,
  29. dateISO: /^\d{4}[\/\-]\d{1,2}[\/\-]\d{1,2}$/,
  30. // MM/DD/YYYY
  31. month_day_year : /^(0[1-9]|1[012])[- \/.](0[1-9]|[12][0-9]|3[01])[- \/.]\d{4}$/,
  32. // #FFF or #FFFFFF
  33. color: /^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/
  34. },
  35. validators : {
  36. equalTo: function(el, required, parent) {
  37. var from = document.getElementById(el.getAttribute(this.add_namespace('data-equalto'))).value,
  38. to = el.value,
  39. valid = (from === to);
  40. return valid;
  41. }
  42. }
  43. },
  44. timer : null,
  45. init : function (scope, method, options) {
  46. this.bindings(method, options);
  47. },
  48. events : function (scope) {
  49. var self = this,
  50. form = self.S(scope).attr('novalidate', 'novalidate'),
  51. settings = form.data(this.attr_name(true) + '-init') || {};
  52. this.invalid_attr = this.add_namespace('data-invalid');
  53. form
  54. .off('.abide')
  55. .on('submit.fndtn.abide validate.fndtn.abide', function (e) {
  56. var is_ajax = /ajax/i.test(self.S(this).attr(self.attr_name()));
  57. return self.validate(self.S(this).find('input, textarea, select').get(), e, is_ajax);
  58. })
  59. .on('reset', function() {
  60. return self.reset($(this));
  61. })
  62. .find('input, textarea, select')
  63. .off('.abide')
  64. .on('blur.fndtn.abide change.fndtn.abide', function (e) {
  65. self.validate([this], e);
  66. })
  67. .on('keydown.fndtn.abide', function (e) {
  68. if (settings.live_validate === true) {
  69. clearTimeout(self.timer);
  70. self.timer = setTimeout(function () {
  71. self.validate([this], e);
  72. }.bind(this), settings.timeout);
  73. }
  74. });
  75. },
  76. reset : function (form) {
  77. form.removeAttr(this.invalid_attr);
  78. $(this.invalid_attr, form).removeAttr(this.invalid_attr);
  79. $('.error', form).not('small').removeClass('error');
  80. },
  81. validate : function (els, e, is_ajax) {
  82. var validations = this.parse_patterns(els),
  83. validation_count = validations.length,
  84. form = this.S(els[0]).closest('form'),
  85. submit_event = /submit/.test(e.type);
  86. // Has to count up to make sure the focus gets applied to the top error
  87. for (var i=0; i < validation_count; i++) {
  88. if (!validations[i] && (submit_event || is_ajax)) {
  89. if (this.settings.focus_on_invalid) els[i].focus();
  90. form.trigger('invalid');
  91. this.S(els[i]).closest('form').attr(this.invalid_attr, '');
  92. return false;
  93. }
  94. }
  95. if (submit_event || is_ajax) {
  96. form.trigger('valid');
  97. }
  98. form.removeAttr(this.invalid_attr);
  99. if (is_ajax) return false;
  100. return true;
  101. },
  102. parse_patterns : function (els) {
  103. var i = els.length,
  104. el_patterns = [];
  105. while (i--) {
  106. el_patterns.push(this.pattern(els[i]));
  107. }
  108. return this.check_validation_and_apply_styles(el_patterns);
  109. },
  110. pattern : function (el) {
  111. var type = el.getAttribute('type'),
  112. required = typeof el.getAttribute('required') === 'string';
  113. var pattern = el.getAttribute('pattern') || '';
  114. if (this.settings.patterns.hasOwnProperty(pattern) && pattern.length > 0) {
  115. return [el, this.settings.patterns[pattern], required];
  116. } else if (pattern.length > 0) {
  117. return [el, new RegExp(pattern), required];
  118. }
  119. if (this.settings.patterns.hasOwnProperty(type)) {
  120. return [el, this.settings.patterns[type], required];
  121. }
  122. pattern = /.*/;
  123. return [el, pattern, required];
  124. },
  125. check_validation_and_apply_styles : function (el_patterns) {
  126. var i = el_patterns.length,
  127. validations = [],
  128. form = this.S(el_patterns[0][0]).closest('[data-' + this.attr_name(true) + ']'),
  129. settings = form.data(this.attr_name(true) + '-init') || {};
  130. while (i--) {
  131. var el = el_patterns[i][0],
  132. required = el_patterns[i][2],
  133. value = el.value.trim(),
  134. direct_parent = this.S(el).parent(),
  135. validator = el.getAttribute(this.add_namespace('data-abide-validator')),
  136. is_radio = el.type === "radio",
  137. is_checkbox = el.type === "checkbox",
  138. label = this.S('label[for="' + el.getAttribute('id') + '"]'),
  139. valid_length = (required) ? (el.value.length > 0) : true;
  140. var parent, valid;
  141. // support old way to do equalTo validations
  142. if(el.getAttribute(this.add_namespace('data-equalto'))) { validator = "equalTo" }
  143. if (!direct_parent.is('label')) {
  144. parent = direct_parent;
  145. } else {
  146. parent = direct_parent.parent();
  147. }
  148. if (is_radio && required) {
  149. validations.push(this.valid_radio(el, required));
  150. } else if (is_checkbox && required) {
  151. validations.push(this.valid_checkbox(el, required));
  152. } else {
  153. if (validator) {
  154. valid = this.settings.validators[validator].apply(this, [el, required, parent]);
  155. validations.push(valid);
  156. }
  157. if (el_patterns[i][1].test(value) && valid_length ||
  158. !required && el.value.length < 1 || $(el).attr('disabled')) {
  159. validations.push(true);
  160. } else {
  161. validations.push(false);
  162. }
  163. validations = [validations.every(function(valid){return valid;})];
  164. if(validations[0]){
  165. this.S(el).removeAttr(this.invalid_attr);
  166. parent.removeClass('error');
  167. if (label.length > 0 && this.settings.error_labels) label.removeClass('error');
  168. $(el).triggerHandler('valid');
  169. } else {
  170. parent.addClass('error');
  171. this.S(el).attr(this.invalid_attr, '');
  172. if (label.length > 0 && this.settings.error_labels) label.addClass('error');
  173. $(el).triggerHandler('invalid');
  174. }
  175. }
  176. }
  177. return validations;
  178. },
  179. valid_checkbox : function(el, required) {
  180. var el = this.S(el),
  181. valid = (el.is(':checked') || !required);
  182. if (valid) {
  183. el.removeAttr(this.invalid_attr).parent().removeClass('error');
  184. } else {
  185. el.attr(this.invalid_attr, '').parent().addClass('error');
  186. }
  187. return valid;
  188. },
  189. valid_radio : function (el, required) {
  190. var name = el.getAttribute('name'),
  191. group = this.S(el).closest('[data-' + this.attr_name(true) + ']').find("[name='"+name+"']"),
  192. count = group.length,
  193. valid = false;
  194. // Has to count up to make sure the focus gets applied to the top error
  195. for (var i=0; i < count; i++) {
  196. if (group[i].checked) valid = true;
  197. }
  198. // Has to count up to make sure the focus gets applied to the top error
  199. for (var i=0; i < count; i++) {
  200. if (valid) {
  201. this.S(group[i]).removeAttr(this.invalid_attr).parent().removeClass('error');
  202. } else {
  203. this.S(group[i]).attr(this.invalid_attr, '').parent().addClass('error');
  204. }
  205. }
  206. return valid;
  207. },
  208. valid_equal: function(el, required, parent) {
  209. var from = document.getElementById(el.getAttribute(this.add_namespace('data-equalto'))).value,
  210. to = el.value,
  211. valid = (from === to);
  212. if (valid) {
  213. this.S(el).removeAttr(this.invalid_attr);
  214. parent.removeClass('error');
  215. } else {
  216. this.S(el).attr(this.invalid_attr, '');
  217. parent.addClass('error');
  218. }
  219. return valid;
  220. },
  221. valid_oneof: function(el, required, parent, doNotValidateOthers) {
  222. var el = this.S(el),
  223. others = this.S('[' + this.add_namespace('data-oneof') + ']'),
  224. valid = others.filter(':checked').length > 0;
  225. if (valid) {
  226. el.removeAttr(this.invalid_attr).parent().removeClass('error');
  227. } else {
  228. el.attr(this.invalid_attr, '').parent().addClass('error');
  229. }
  230. if (!doNotValidateOthers) {
  231. var _this = this;
  232. others.each(function() {
  233. _this.valid_oneof.call(_this, this, null, null, true);
  234. });
  235. }
  236. return valid;
  237. }
  238. };
  239. }(jQuery, window, window.document));