-!function(Foundation, $) {
- 'use strict';
+'use strict';
+export default class Abide {
/**
* Creates a new instance of Abide.
* @class
* @param {Object} element - jQuery object to add the trigger to.
* @param {Object} options - Overrides to the default plugin settings.
*/
- function Abide(element, options) {
+ constructor(element, options) {
this.$element = element;
this.options = $.extend({}, Abide.defaults, this.$element.data(), options);
Foundation.registerPlugin(this, 'Abide');
}
- /**
- * Default settings for plugin
- */
- Abide.defaults = {
- /**
- * The default event to validate inputs. Checkboxes and radios validate immediately.
- * Remove or change this value for manual validation.
- * @option
- * @example 'fieldChange'
- */
- validateOn: 'fieldChange',
- /**
- * Class to be applied to input labels on failed validation.
- * @option
- * @example 'is-invalid-label'
- */
- labelErrorClass: 'is-invalid-label',
- /**
- * Class to be applied to inputs on failed validation.
- * @option
- * @example 'is-invalid-input'
- */
- inputErrorClass: 'is-invalid-input',
- /**
- * Class selector to use to target Form Errors for show/hide.
- * @option
- * @example '.form-error'
- */
- formErrorSelector: '.form-error',
- /**
- * Class added to Form Errors on failed validation.
- * @option
- * @example 'is-visible'
- */
- formErrorClass: 'is-visible',
- /**
- * Set to true to validate text inputs on any value change.
- * @option
- * @example false
- */
- 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-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FFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([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})$/
- },
- /**
- * Optional validation functions to be used. `equalTo` being the only default included function.
- * Functions should return only a boolean if the input is valid or not. Functions are given the following arguments:
- * el : The jQuery element to validate.
- * required : Boolean value of the required attribute be present or not.
- * parent : The direct parent of the input.
- * @option
- */
- 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(){
+ _init(){
this.$inputs = this.$element.find('input, textarea, select').not('[data-abide-ignore]');
this._events();
- };
+ }
/**
* Initializes events for Abide.
* @private
*/
- Abide.prototype._events = function() {
+ _events() {
var _this = this;
this.$element.off('.abide')
_this.validateInput($(this));
});
}
- },
+ }
+
/**
* Calls necessary functions to update Abide upon DOM change
* @private
*/
- Abide.prototype._reflow = function() {
+ _reflow() {
this._init();
- };
+ }
+
/**
* 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
*/
- Abide.prototype.requiredCheck = function($el) {
+ requiredCheck($el) {
if(!$el.attr('required')) return true;
var isGood = true;
switch ($el[0].type) {
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's direct sibling('s).
* @param {Object} $el - jQuery object to use as reference to find the form error selector.
* @returns {Object} jQuery object with the selector.
*/
- Abide.prototype.findFormError = function($el){
+ findFormError($el) {
var $error = $el.siblings(this.options.formErrorSelector);
if(!$error.length){
$error = $el.parent().find(this.options.formErrorSelector);
}
return $error;
- };
+ }
+
/**
* Get the first element in this order:
* 2. The <label> with the attribute `[for="someInputId"]`
* @param {Object} $el - jQuery object to check for required attribute
* @returns {Boolean} Boolean value depends on whether or not attribute is checked or empty
*/
- Abide.prototype.findLabel = function($el) {
+ findLabel($el) {
var $label = this.$element.find(`label[for="${$el[0].id}"]`);
if(!$label.length){
return $el.closest('label');
}
return $label;
- };
+ }
+
/**
* Adds the CSS error class as specified by the Abide settings to the label, input, and the form
* @param {Object} $el - jQuery object to add the class to
*/
- Abide.prototype.addErrorClasses = function($el){
+ addErrorClasses($el) {
var $label = this.findLabel($el),
$formError = this.findFormError($el);
$formError.addClass(this.options.formErrorClass);
}
$el.addClass(this.options.inputErrorClass).attr('data-invalid', '');
- };
+ }
+
/**
* Removes CSS error class as specified by the Abide settings from the label, input, and the form
* @param {Object} $el - jQuery object to remove the class from
*/
- Abide.prototype.removeErrorClasses = function($el){
+ removeErrorClasses($el) {
var $label = this.findLabel($el),
$formError = this.findFormError($el);
$formError.removeClass(this.options.formErrorClass);
}
$el.removeClass(this.options.inputErrorClass).removeAttr('data-invalid');
- };
+ }
+
/**
* Goes through a form to find inputs and proceeds to validate them in ways specific to their type
* @fires Abide#invalid
* @param {Object} element - jQuery object to validate, should be an HTML input
* @returns {Boolean} goodToGo - If the input is valid or not.
*/
- Abide.prototype.validateInput = function($el){
+ validateInput($el) {
var clearRequire = this.requiredCheck($el),
validated = false,
customValidator = true,
$el.trigger(message, [$el]);
return goodToGo;
- };
+ }
+
/**
* Goes through a form and if there are any invalid inputs, it will display the form error element
* @returns {Boolean} noError - true if no errors were detected...
* @fires Abide#formvalid
* @fires Abide#forminvalid
*/
- Abide.prototype.validateForm = function(){
+ validateForm() {
var acc = [],
_this = this;
this.$element.trigger((noError ? 'formvalid' : 'forminvalid') + '.zf.abide', [this.$element]);
return noError;
- };
+ }
+
/**
* Determines whether or a not a text input is valid based on the pattern specified in the attribute. If no matching pattern is found, returns true.
* @param {Object} $el - jQuery object to validate, should be a text input HTML element
* @param {String} pattern - string value of one of the RegEx patterns in Abide.options.patterns
* @returns {Boolean} Boolean value depends on whether or not the input value matches the pattern specified
*/
- Abide.prototype.validateText = function($el, pattern){
+ validateText($el, pattern) {
// pattern = pattern ? pattern : $el.attr('pattern') ? $el.attr('pattern') : $el.attr('type');
pattern = (pattern || $el.attr('pattern') || $el.attr('type'));
var inputText = $el.val();
return inputText.length ?//if text, check if the pattern exists, if so, test it, if no text or no pattern, return true.
this.options.patterns.hasOwnProperty(pattern) ? this.options.patterns[pattern].test(inputText) :
pattern && pattern !== $el.attr('type') ? new RegExp(pattern).test(inputText) : true : true;
- }; /**
+ }
+
+ /**
* Determines whether or a not a radio input is valid based on whether or not it is required and selected
* @param {String} groupName - A string that specifies the name of a radio button group
* @returns {Boolean} Boolean value depends on whether or not at least one radio input has been selected (if it's required)
*/
- Abide.prototype.validateRadio = function(groupName){
+ validateRadio(groupName) {
var $group = this.$element.find(`:radio[name="${groupName}"]`),
counter = [],
_this = this;
});
return counter.indexOf(false) === -1;
- };
+ }
+
/**
* Determines if a selected input passes a custom validation function. Multiple validations can be used, if passed to the element with `data-validator="foo bar baz"` in a space separated listed.
* @param {Object} $el - jQuery input element.
* @param {Boolean} required - self explanatory?
* @returns {Boolean} - true if validations passed.
*/
- Abide.prototype.matchValidation = function($el, validators, required){
+ matchValidation($el, validators, required) {
var _this = this;
required = required ? true : false;
var clear = validators.split(' ').map(function(v){
return _this.options.validators[v]($el, required, $el.parent());
});
return clear.indexOf(false) === -1;
- };
+ }
+
/**
* Resets form inputs and styles
* @fires Abide#formreset
*/
- Abide.prototype.resetForm = function() {
+ resetForm() {
var $form = this.$element,
opts = this.options;
* @event Abide#formreset
*/
$form.trigger('formreset.zf.abide', [$form]);
- };
+ }
+
/**
* Destroys an instance of Abide.
* Removes error styles and classes from elements, without resetting their values.
*/
- Abide.prototype.destroy = function(){
+ destroy() {
var _this = this;
this.$element.off('.abide')
.find('[data-abide-error]').css('display', 'none');
});
Foundation.unregisterPlugin(this);
- };
-
- Foundation.plugin(Abide, 'Abide');
-
- // Exports for AMD/Browserify
- if (typeof module !== 'undefined' && typeof module.exports !== 'undefined')
- module.exports = Abide;
- if (typeof define === 'function')
- define(['foundation'], function() {
- return Abide;
- });
+ }
+}
-}(Foundation, jQuery);
+/**
+ * Default settings for plugin
+ */
+Abide.defaults = {
+ /**
+ * The default event to validate inputs. Checkboxes and radios validate immediately.
+ * Remove or change this value for manual validation.
+ * @option
+ * @example 'fieldChange'
+ */
+ validateOn: 'fieldChange',
+ /**
+ * Class to be applied to input labels on failed validation.
+ * @option
+ * @example 'is-invalid-label'
+ */
+ labelErrorClass: 'is-invalid-label',
+ /**
+ * Class to be applied to inputs on failed validation.
+ * @option
+ * @example 'is-invalid-input'
+ */
+ inputErrorClass: 'is-invalid-input',
+ /**
+ * Class selector to use to target Form Errors for show/hide.
+ * @option
+ * @example '.form-error'
+ */
+ formErrorSelector: '.form-error',
+ /**
+ * Class added to Form Errors on failed validation.
+ * @option
+ * @example 'is-visible'
+ */
+ formErrorClass: 'is-visible',
+ /**
+ * Set to true to validate text inputs on any value change.
+ * @option
+ * @example false
+ */
+ 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])?)+$/,
+
+ url : /^(https?|ftp|file|ssh):\/\/(((([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-zA-Z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-zA-Z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-zA-Z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-zA-Z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-zA-Z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-zA-Z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([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})$/
+ },
+ /**
+ * Optional validation functions to be used. `equalTo` being the only default included function.
+ * Functions should return only a boolean if the input is valid or not. Functions are given the following arguments:
+ * el : The jQuery element to validate.
+ * required : Boolean value of the required attribute be present or not.
+ * parent : The direct parent of the input.
+ * @option
+ */
+ validators: {
+ equalTo: function (el, required, parent) {
+ return $(`#${el.attr('data-equalto')}`).val() === el.val();
+ }
+ }
+};
+
+// Window exports
+if (window.Foundation) {
+ window.Foundation.plugin(Abide, 'Abide');
+}
+
+// Exports for AMD/Browserify
+if (typeof module !== 'undefined' && typeof module.exports !== 'undefined')
+ module.exports = Abide;
+if (typeof define === 'function')
+ define(['foundation'], function() {
+ return Abide;
+ });