From 550c483a32612b51a0df5142af8656470e46714c Mon Sep 17 00:00:00 2001 From: Chris Oyler Date: Tue, 15 Dec 2015 16:40:44 -0800 Subject: [PATCH] new abide in the works --- js/foundation.new-abide.js | 293 +++++++++++++++++++++++++++++++++++++ 1 file changed, 293 insertions(+) create mode 100644 js/foundation.new-abide.js diff --git a/js/foundation.new-abide.js b/js/foundation.new-abide.js new file mode 100644 index 000000000..13b3610e6 --- /dev/null +++ b/js/foundation.new-abide.js @@ -0,0 +1,293 @@ +!function(Foundation, $) { + 'use strict'; + + /** + * Creates a new instance of Abide. + * @class + * @fires Abide#init + * @param {Object} element - jQuery object to add the trigger to. + * @param {Object} options - Overrides to the default plugin settings. + */ + function Abide(element, options) { + this.$element = element; + this.options = $.extend({}, Abide.defaults, this.$element.data(), options); + + this._init(); + + Foundation.registerPlugin(this); + } + + /** + * Default settings for plugin + */ + Abide.defaults = { + validateOn: 'fieldChange', // options: fieldChange, manual, submit + labelErrorClass: 'is-invalid-label', + inputErrorClass: 'is-invalid-input', + formErrorSelector: '.form-error', + formErrorClass: 'is-visible', + liveValidate: false, + + patterns: { + alpha : /^[a-zA-Z]+$/, + alpha_numeric : /^[a-zA-Z0-9]+$/, + integer : /^[-+]?\d+$/, + number : /^[-+]?\d*(?:[\.\,]\d+)?$/, + + // amex, visa, diners + 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})$/, + cvv : /^([0-9]){3,4}$/, + + // http://www.whatwg.org/specs/web-apps/current-work/multipage/states-of-the-type-attribute.html#valid-e-mail-address + 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])?)+$/, + +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8FF]|\/|\?)*)?(\#((([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/, + // abc.de + domain : /^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,8}$/, + + 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))$/, + // YYYY-MM-DD + 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))$/, + // HH:MM:SS + time : /^(0[0-9]|1[0-9]|2[0-3])(:[0-5][0-9]){2}$/, + dateISO : /^\d{4}[\/\-]\d{1,2}[\/\-]\d{1,2}$/, + // MM/DD/YYYY + month_day_year : /^(0[1-9]|1[012])[- \/.](0[1-9]|[12][0-9]|3[01])[- \/.]\d{4}$/, + // DD/MM/YYYY + day_month_year : /^(0[1-9]|[12][0-9]|3[01])[- \/.](0[1-9]|1[012])[- \/.]\d{4}$/, + + // #FFF or #FFFFFF + color : /^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/ + }, + + validators: { + equalTo: function (el, required, parent) { + return $('#' + el.attr('data-equalto')).val() === el.val(); + } + } + }; + + + /** + * Initializes the Abide plugin and calls functions to get Abide functioning on load. + * @private + */ + Abide.prototype._init = function(){ + this.$inputs = this.$element.find('input, textarea, select'); + this._events(); + }; + + /** + * Initializes events for Abide. + * @private + */ + Abide.prototype._events = function() { + var _this = this; + + this.$element.off('.abide') + .on('reset.zf.abide reset.fndtn.abide', function(e){ + _this.resetForm($(this)); + }) + .on('submit.zf.abide submit.fndtn.abide', function(e){ + _this.validateForm(); + }); + + this.$inputs.off('.abide') + .on('change.zf.abide', function(e){ + if(_this.options.validateOn === 'fieldChange') _this.validateInput($(this)); + }); + + if(this.options.liveValidate){ + this.$inputs.on('input.zf.abide', function(e){ + _this.validateInput($(this)); + }); + } + }, + /** + * Calls necessary functions to update Abide upon DOM change + * @private + */ + Abide.prototype._reflow = function() { + var self = this; + }; + /** + * Checks whether or not a form element has the required attribute and if it's checked or not + * @param {Object} element - jQuery object to check for required attribute + * @returns {Boolean} Boolean value depends on whether or not attribute is checked or empty + */ + var counter = 0; + Abide.prototype.requiredCheck = function($el) { + if(!$el.attr('required')) return true; + var isGood = true; + switch ($el[0].type) { + + case 'checkbox': + case 'radio': + isGood = $el[0].checked; + break; + + case 'select': + case 'select-one': + var opt = $el.find('option:selected'); + if(!opt.length || !opt.val()) isGood = false; + break; + + default: + if(!$el.val() || !$el.val().length) isGood = false; + } + return isGood; + }; + /** + * Based on $el, get the first element with selector in this order: + * 1. The element next to it. + * 2. The element inside (is a child of) it. + * 3. As its sibling. + * + * @param {Object} element - jQuery object to use as reference to find the form error selector. + * @param {String} selector - jQuery selector to show error message when field doesn't pass validation. + * @returns {Object} jQuery object with the selector. + */ + Abide.prototype.findFormError = function($el, selector) { + }; + /** + * Get the first element in this order: + * 1. The closest parent ".input-group" class. + * 2. The