vendor/assets/javascripts/formvalidation.js in formvalidation-rails-0.6.3 vs vendor/assets/javascripts/formvalidation.js in formvalidation-rails-0.7.0

- old
+ new

@@ -1,11 +1,11 @@ /*! * FormValidation (http://formvalidation.io) * The best jQuery plugin to validate form fields. Support Bootstrap, Foundation, Pure, SemanticUI, UIKit and custom frameworks * - * @version v0.6.2-dev, built on 2015-03-13 8:15:45 AM - * @author https://twitter.com/nghuuphuoc + * @version v0.7.0, built on 2015-08-01 4:57:16 PM + * @author https://twitter.com/formvalidation * @copyright (c) 2013 - 2015 Nguyen Huu Phuoc * @license http://formvalidation.io/license/ */ // Register the namespace window.FormValidation = { @@ -25,11 +25,11 @@ throw new Error('FormValidation requires jQuery version 1.9.1 or higher'); } }(jQuery)); (function($) { - // TODO: Remove backward compatibility in v0.7.0 + // TODO: Remove backward compatibility /** * Constructor * * @param {jQuery|String} form The form element or selector * @param {Object} options The options @@ -51,23 +51,26 @@ this.STATUS_VALIDATING = 'VALIDATING'; this.STATUS_INVALID = 'INVALID'; this.STATUS_VALID = 'VALID'; this.STATUS_IGNORED = 'IGNORED'; + // Default message + this.DEFAULT_MESSAGE = $.fn.formValidation.DEFAULT_MESSAGE; + // Determine the event that is fired when user change the field value // Most modern browsers supports input event except IE 7, 8. // IE 9 supports input event but the event is still not fired if I press the backspace key. // Get IE version // https://gist.github.com/padolsey/527683/#comment-7595 - var ieVersion = (function() { + this._ieVersion = (function() { var v = 3, div = document.createElement('div'), a = div.all || []; while (div.innerHTML = '<!--[if gt IE '+(++v)+']><br><![endif]-->', a[0]) {} - return v > 4 ? v : !v; + return v > 4 ? v : document.documentMode; }()); var el = document.createElement('div'); - this._changeEvent = (ieVersion === 9 || !('oninput' in el)) ? 'keyup' : 'input'; + this._changeEvent = (this._ieVersion === 9 || !('oninput' in el)) ? 'keyup' : 'input'; // The flag to indicate that the form is ready to submit when a remote/callback validator returns this._submitIfValid = null; // Field elements @@ -118,10 +121,11 @@ container: this.$form.attr('data-' + ns + '-err-container') || this.$form.attr('data-' + ns + '-container'), // Support backward parent: this.$form.attr('data-' + ns + '-err-parent') }, events: { formInit: this.$form.attr('data-' + ns + '-events-form-init'), + formPreValidate: this.$form.attr('data-' + ns + '-events-form-prevalidate'), formError: this.$form.attr('data-' + ns + '-events-form-error'), formSuccess: this.$form.attr('data-' + ns + '-events-form-success'), fieldAdded: this.$form.attr('data-' + ns + '-events-field-added'), fieldRemoved: this.$form.attr('data-' + ns + '-events-field-removed'), fieldInit: this.$form.attr('data-' + ns + '-events-field-init'), @@ -141,10 +145,11 @@ feedback: this.$form.attr('data-' + ns + '-icon-feedback') }, live: this.$form.attr('data-' + ns + '-live'), locale: this.$form.attr('data-' + ns + '-locale'), message: this.$form.attr('data-' + ns + '-message'), + onPreValidate: this.$form.attr('data-' + ns + '-onprevalidate'), onError: this.$form.attr('data-' + ns + '-onerror'), onSuccess: this.$form.attr('data-' + ns + '-onsuccess'), row: { selector: this.$form.attr('data-' + ns + '-row-selector') || this.$form.attr('data-' + ns + '-group'), // Support backward valid: this.$form.attr('data-' + ns + '-row-valid'), @@ -174,11 +179,11 @@ if (this.options.declarative === true || this.options.declarative === 'true') { // Find all fields which have either "name" or "data-{namespace}-field" attribute this.$form .find('[name], [data-' + ns + '-field]') - .each(function () { + .each(function() { var $field = $(this), field = $field.attr('name') || $field.attr('data-' + ns + '-field'), opts = that._parseOptions($field); if (opts) { $field.attr('data-' + ns + '-field', field); @@ -237,12 +242,12 @@ if (!e.isDefaultPrevented()) { var $target = $(e.target), // The button might contain HTML tag $button = $target.is('[type="submit"]') ? $target.eq(0) : $target.parent('[type="submit"]').eq(0); - // Don't perform validation when clicking on the submit button/input - // which aren't defined by the 'button.selector' option + // Don't perform validation when clicking on the submit button/input which + // aren't defined by the 'button.selector' option if (that.options.button.selector && !$button.is(that.options.button.selector) && !$button.is(that.$hiddenButton)) { that.$form.off('submit.' + that._namespace).submit(); } } }); @@ -263,10 +268,15 @@ fv: this, options: this.options }); // Prepare the events + if (this.options.onPreValidate) { + this.$form.on(this.options.events.formPreValidate, function(e) { + FormValidation.Helper.call(that.options.onPreValidate, [e]); + }); + } if (this.options.onSuccess) { this.$form.on(this.options.events.formSuccess, function(e) { FormValidation.Helper.call(that.options.onSuccess, [e]); }); } @@ -305,13 +315,16 @@ if (this.options.fields[field] === null || this.options.fields[field].validators === null) { return; } - var validatorName; - for (validatorName in this.options.fields[field].validators) { - if (!FormValidation.Validator[validatorName]) { + var validators = this.options.fields[field].validators, + validatorName, + alias; + for (validatorName in validators) { + alias = validators[validatorName].alias || validatorName; + if (!FormValidation.Validator[alias]) { delete this.options.fields[field].validators[validatorName]; } } if (this.options.fields[field].enabled === null) { this.options.fields[field].enabled = true; @@ -350,11 +363,11 @@ that.updateStatus($(this), that.STATUS_NOT_VALIDATED); }); // Create help block elements for showing the error messages $field.data(ns + '.messages', $message); - for (validatorName in this.options.fields[field].validators) { + for (validatorName in validators) { $field.data(ns + '.result.' + validatorName, this.STATUS_NOT_VALIDATED); if (!updateAll || i === total - 1) { $('<small/>') .css('display', 'none') @@ -365,12 +378,13 @@ .html(this._getMessage(field, validatorName)) .appendTo($message); } // Init the validator - if ('function' === typeof FormValidation.Validator[validatorName].init) { - FormValidation.Validator[validatorName].init(this, $field, this.options.fields[field].validators[validatorName]); + alias = validators[validatorName].alias || validatorName; + if ('function' === typeof FormValidation.Validator[alias].init) { + FormValidation.Validator[alias].init(this, $field, this.options.fields[field].validators[validatorName], validatorName); } } // Prepare the feedback icons if (this.options.fields[field].icon !== false && this.options.fields[field].icon !== 'false' @@ -401,16 +415,16 @@ $field // Show tooltip/popover message when field gets focus .off('focus.container.' + ns) .on('focus.container.' + ns, function() { - that._showTooltip($field, container); + that._showTooltip($(this), container); }) // and hide them when losing focus .off('blur.container.' + ns) .on('blur.container.' + ns, function() { - that._hideTooltip($field, container); + that._hideTooltip($(this), container); }); } if ('string' === typeof this.options.fields[field].icon && this.options.fields[field].icon !== 'true') { $icon.appendTo($(this.options.fields[field].icon)); @@ -444,10 +458,16 @@ var onError = that.getOptions(data.field, data.validator, 'onError'); if (onError) { FormValidation.Helper.call(onError, [e, data]); } }) + .on(this.options.events.validatorIgnored, function(e, data) { + var onIgnored = that.getOptions(data.field, data.validator, 'onIgnored'); + if (onIgnored) { + FormValidation.Helper.call(onIgnored, [e, data]); + } + }) .on(this.options.events.validatorSuccess, function(e, data) { var onSuccess = that.getOptions(data.field, data.validator, 'onSuccess'); if (onSuccess) { FormValidation.Helper.call(onSuccess, [e, data]); } @@ -532,13 +552,17 @@ trigger = $field.data(ns + '.trigger'); if (trigger) { return trigger; } + // IE10/11 auto fires input event of elements using the placeholder attribute + // https://connect.microsoft.com/IE/feedback/details/856700/ var type = $field.attr('type'), name = $field.attr('data-' + ns + '-field'), - event = ('radio' === type || 'checkbox' === type || 'file' === type || 'SELECT' === $field.get(0).tagName) ? 'change' : this._changeEvent; + event = ('radio' === type || 'checkbox' === type || 'file' === type || 'SELECT' === $field.get(0).tagName) + ? 'change' + : (this._ieVersion >= 10 && $field.attr('placeholder') ? 'keyup' : this._changeEvent); trigger = ((this.options.fields[name] ? this.options.fields[name].trigger : null) || this.options.trigger || event).split(' '); // Since the trigger data is used many times, I need to cache it to use later $field.data(ns + '.trigger', trigger); @@ -551,25 +575,30 @@ * @param {String} field The field name * @param {String} validatorName The validator name * @returns {String} */ _getMessage: function(field, validatorName) { - if (!this.options.fields[field] || !FormValidation.Validator[validatorName] - || !this.options.fields[field].validators || !this.options.fields[field].validators[validatorName]) - { + if (!this.options.fields[field] || !this.options.fields[field].validators) { return ''; } + var validators = this.options.fields[field].validators, + alias = (validators[validatorName] && validators[validatorName].alias) ? validators[validatorName].alias : validatorName; + if (!FormValidation.Validator[alias]) { + return ''; + } switch (true) { - case !!this.options.fields[field].validators[validatorName].message: - return this.options.fields[field].validators[validatorName].message; + case !!validators[validatorName].message: + return validators[validatorName].message; case !!this.options.fields[field].message: return this.options.fields[field].message; - case (!!FormValidation.I18n[this.options.locale] && !!FormValidation.I18n[this.options.locale][validatorName] && !!FormValidation.I18n[this.options.locale][validatorName]['default']): - return FormValidation.I18n[this.options.locale][validatorName]['default']; - default: + case !!this.options.message: return this.options.message; + case (!!FormValidation.I18n[this.options.locale] && !!FormValidation.I18n[this.options.locale][alias] && !!FormValidation.I18n[this.options.locale][alias]['default']): + return FormValidation.I18n[this.options.locale][alias]['default']; + default: + return this.DEFAULT_MESSAGE; } }, /** * Get the element to place the error messages @@ -647,25 +676,38 @@ * * @param {jQuery} $field The field element * @returns {Object} */ _parseOptions: function($field) { - var ns = this._namespace, - field = $field.attr('name') || $field.attr('data-' + ns + '-field'), - validators = {}, + var ns = this._namespace, + field = $field.attr('name') || $field.attr('data-' + ns + '-field'), + validators = {}, + aliasAttr = new RegExp('^data-' + ns + '-([a-z]+)-alias$'), + validatorSet = $.extend({}, FormValidation.Validator), validator, v, // Validator name attrName, enabled, optionName, optionAttrName, optionValue, html5AttrName, html5AttrMap; - for (v in FormValidation.Validator) { - validator = FormValidation.Validator[v]; + // Determine whether the alias validator is used by checking the data-fv-validator-alias attribute + $.each($field.get(0).attributes, function(i, attribute) { + if (attribute.value && aliasAttr.test(attribute.name)) { + v = attribute.name.split('-')[2]; + if (validatorSet[attribute.value]) { + validatorSet[v] = validatorSet[attribute.value]; + validatorSet[v].alias = attribute.value; + } + } + }); + + for (v in validatorSet) { + validator = validatorSet[v]; attrName = 'data-' + ns + '-' + v.toLowerCase(), enabled = $field.attr(attrName) + ''; html5AttrMap = ('function' === typeof validator.enableByHtml5) ? validator.enableByHtml5($field) : null; if ((html5AttrMap && enabled !== 'false') @@ -677,15 +719,18 @@ onerror: 'onError', onsuccess: 'onSuccess', transformer: 'transformer' }, validator.html5Attributes); validators[v] = $.extend({}, html5AttrMap === true ? {} : html5AttrMap, validators[v]); + if (validator.alias) { + validators[v].alias = validator.alias; + } for (html5AttrName in validator.html5Attributes) { - optionName = validator.html5Attributes[html5AttrName]; - optionAttrName = 'data-' + ns + '-' + v.toLowerCase() + '-' + html5AttrName, - optionValue = $field.attr(optionAttrName); + optionName = validator.html5Attributes[html5AttrName]; + optionAttrName = 'data-' + ns + '-' + v.toLowerCase() + '-' + html5AttrName; + optionValue = $field.attr(optionAttrName); if (optionValue) { if ('true' === optionValue || optionAttrName === optionValue.toLowerCase()) { optionValue = true; } else if ('false' === optionValue) { optionValue = false; @@ -697,10 +742,11 @@ } var opts = { autoFocus: $field.attr('data-' + ns + '-autofocus'), err: $field.attr('data-' + ns + '-err-container') || $field.attr('data-' + ns + '-container'), // Support backward + enabled: $field.attr('data-' + ns + '-enabled'), excluded: $field.attr('data-' + ns + '-excluded'), icon: $field.attr('data-' + ns + '-icon') || $field.attr('data-' + ns + '-feedbackicons') || (this.options.fields && this.options.fields[field] ? this.options.fields[field].feedbackIcons : null), // Support backward message: $field.attr('data-' + ns + '-message'), onError: $field.attr('data-' + ns + '-onerror'), onStatus: $field.attr('data-' + ns + '-onstatus'), @@ -715,11 +761,11 @@ }, emptyOptions = $.isEmptyObject(opts), // Check if the field options are set using HTML attributes emptyValidators = $.isEmptyObject(validators); // Check if the field validators are set using HTML attributes if (!emptyValidators || (!emptyOptions && this.options.fields && this.options.fields[field])) { - opts.validators = validators; + //opts.validators = validators; return opts; } else { return null; } }, @@ -1159,10 +1205,16 @@ var $f = fields[i], field = $f.attr('data-' + ns + '-field'), $errors = $f.data(ns + '.messages') .find('.' + this.options.err.clazz.split(' ').join('.') + '[data-' + ns + '-validator][data-' + ns + '-for="' + field + '"]'); + if (this.options.fields && this.options.fields[field] + && (this.options.fields[field].enabled === 'false' || this.options.fields[field].enabled === false)) + { + continue; + } + if ($errors.filter('[data-' + ns + '-result="' + this.STATUS_INVALID + '"]').length > 0) { return false; } // If the field is not validated @@ -1197,11 +1249,13 @@ fields = this.getFieldElements(field); break; default: break; } - if (fields.length === 0 || !this.options.fields[field] || this.options.fields[field].enabled === false) { + if (fields.length === 0 || !this.options.fields[field] + || this.options.fields[field].enabled === 'false' || this.options.fields[field].enabled === false) + { return true; } var type = fields.attr('type'), total = ('radio' === type || 'checkbox' === type) ? 1 : fields.length, @@ -1409,20 +1463,22 @@ if ($icon) { $icon.removeClass(this.options.icon.valid).removeClass(this.options.icon.validating).addClass(this.options.icon.invalid).show(); } break; - case this.STATUS_VALID: case this.STATUS_IGNORED: // Treat ignored fields like they are valid with some specialties + case this.STATUS_VALID: isValidating = ($allErrors.filter('[data-' + ns + '-result="' + this.STATUS_VALIDATING +'"]').length > 0); isNotValidated = ($allErrors.filter('[data-' + ns + '-result="' + this.STATUS_NOT_VALIDATED +'"]').length > 0); + var numIgnored = $allErrors.filter('[data-' + ns + '-result="' + this.STATUS_IGNORED +'"]').length; + // If the field is valid (passes all validators) isValidField = (isValidating || isNotValidated) // There are some validators that have not done - ? null - : ($allErrors.filter('[data-' + ns + '-result="' + this.STATUS_VALID +'"]').length - + $allErrors.filter('[data-' + ns + '-result="' + this.STATUS_IGNORED +'"]').length === $allErrors.length); // All validators are completed + ? null + // All validators are completed + : ($allErrors.filter('[data-' + ns + '-result="' + this.STATUS_VALID +'"]').length + numIgnored === $allErrors.length); $field.removeClass(this.options.control.valid).removeClass(this.options.control.invalid); if (isValidField === true) { this.disableSubmitButtons(this.isValid() === false); @@ -1436,22 +1492,22 @@ } } if ($icon) { $icon.removeClass(this.options.icon.invalid).removeClass(this.options.icon.validating).removeClass(this.options.icon.valid); - if (status === this.STATUS_VALID) { - $icon.addClass(isValidField === null - ? '' : (isValidField ? this.options.icon.valid : (isValidating ? this.options.icon.validating : this.options.icon.invalid))) + if (status === this.STATUS_VALID || numIgnored !== $allErrors.length) { + $icon.addClass(isValidating + ? this.options.icon.validating + : (isValidField === null ? '' : (isValidField ? this.options.icon.valid : this.options.icon.invalid))) .show(); } } var isValidContainer = this.isValidContainer($parent); if (isValidContainer !== null) { $parent.removeClass(this.options.row.valid).removeClass(this.options.row.invalid); - - if (status === this.STATUS_VALID || $allErrors.length > 1) { + if (status === this.STATUS_VALID || numIgnored !== $allErrors.length) { $parent.addClass(isValidContainer ? this.options.row.valid : this.options.row.invalid); } } break; @@ -1499,12 +1555,13 @@ validate: function() { if ($.isEmptyObject(this.options.fields)) { this._submit(); return this; } - this.disableSubmitButtons(true); + this.$form.trigger($.Event(this.options.events.formPreValidate)); + this.disableSubmitButtons(true); this._submitIfValid = false; for (var field in this.options.fields) { this.validateField(field); } @@ -1533,21 +1590,24 @@ break; default: break; } - if (fields.length === 0 || !this.options.fields[field] || this.options.fields[field].enabled === false) { + if (fields.length === 0 || !this.options.fields[field] + || this.options.fields[field].enabled === 'false' || this.options.fields[field].enabled === false) + { return this; } var that = this, type = fields.attr('type'), total = ('radio' === type || 'checkbox' === type) ? 1 : fields.length, updateAll = ('radio' === type || 'checkbox' === type), validators = this.options.fields[field].validators, verbose = this.isOptionEnabled(field, 'verbose'), validatorName, + alias, validateResult; for (var i = 0; i < total; i++) { var $field = fields.eq(i); if (this._isExcluded($field)) { @@ -1567,17 +1627,23 @@ var result = $field.data(ns + '.result.' + validatorName); if (result === this.STATUS_VALID || result === this.STATUS_INVALID) { this._onFieldValidated($field, validatorName); continue; } else if (validators[validatorName].enabled === false) { - this.updateStatus(updateAll ? field : $field, this.STATUS_VALID, validatorName); + // Changed in v0.6.2: + // When the field validator is disabled, it should be treated as STATUS_IGNORED instead of STATUS_VALID + // By doing that, the field with only disabled and ignored validators will not have success/error class + this.updateStatus(updateAll ? field : $field, this.STATUS_IGNORED, validatorName); continue; } $field.data(ns + '.result.' + validatorName, this.STATUS_VALIDATING); - validateResult = FormValidation.Validator[validatorName].validate(this, $field, validators[validatorName]); + // Check whether or not the validator is just an alias of another + alias = validators[validatorName].alias || validatorName; + validateResult = FormValidation.Validator[alias].validate(this, $field, validators[validatorName], validatorName); + // validateResult can be a $.Deferred object ... if ('object' === typeof validateResult && validateResult.resolve) { this.updateStatus(updateAll ? field : $field, this.STATUS_VALIDATING, validatorName); $field.data(ns + '.dfs.' + validatorName, validateResult); @@ -1619,13 +1685,13 @@ this.updateStatus(updateAll ? field : $field, validateResult ? this.STATUS_VALID : this.STATUS_INVALID, validatorName); if (!validateResult && !verbose) { break; } } - // ... or null/undefined - // to indicate that the field should be ignored for current validation - else if (null === validateResult || undefined === validateResult) { + // ... or null + // to indicate that the field should be ignored for current validator + else if (null === validateResult) { $field.data(ns + '.response.' + validatorName, validateResult); this.updateStatus(updateAll ? field : $field, this.STATUS_IGNORED, validatorName); } } } @@ -1695,11 +1761,11 @@ /** * Destroy the plugin * It will remove all error messages, feedback icons and turn off the events */ destroy: function() { - var ns = this._namespace, i, field, fields, $field, validator, $icon, row; + var ns = this._namespace, i, field, fields, $field, validator, $icon, row, alias; // Destroy the validators first for (field in this.options.fields) { fields = this.getFieldElements(field); for (i = 0; i < fields.length; i++) { @@ -1711,12 +1777,13 @@ $field.removeData(ns + '.result.' + validator) .removeData(ns + '.response.' + validator) .removeData(ns + '.dfs.' + validator); // Destroy the validator - if ('function' === typeof FormValidation.Validator[validator].destroy) { - FormValidation.Validator[validator].destroy(this, $field, this.options.fields[field].validators[validator]); + alias = this.options.fields[field].validators[validator].alias || validator; + if ('function' === typeof FormValidation.Validator[alias].destroy) { + FormValidation.Validator[alias].destroy(this, $field, this.options.fields[field].validators[validator], validator); } } } } @@ -2171,12 +2238,14 @@ }); }; $.fn.formValidation.Constructor = FormValidation.Base; - // The default options - // Sorted in alphabetical order + // Default message + $.fn.formValidation.DEFAULT_MESSAGE = 'This value is not valid'; + + // The default options sorted in alphabetical order $.fn.formValidation.DEFAULT_OPTIONS = { // The first invalid field will be focused automatically autoFocus: true, // Support declarative usage (setting options via HTML 5 attributes) @@ -2189,10 +2258,11 @@ // Use custom event name to avoid window.onerror being invoked by jQuery // See #630 events: { // Support backward formInit: 'init.form.fv', + formPreValidate: 'prevalidate.form.fv', formError: 'err.form.fv', formSuccess: 'success.form.fv', fieldAdded: 'added.field.fv', fieldRemoved: 'removed.field.fv', fieldInit: 'init.field.fv', @@ -2242,11 +2312,11 @@ // Locale in the format of languagecode_COUNTRYCODE locale: 'en_US', // Default invalid message - message: 'This value is not valid', + message: null, // The field will not be live validated if its length is less than this number of characters threshold: null, // Whether to be verbose when validating a field or not. @@ -2262,11 +2332,12 @@ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ button: { // The submit buttons selector // These buttons will be disabled to prevent the valid form from multiple submissions - selector: '[type="submit"]', + // Don't perform validation when clicking on the submit button/input which have formnovalidate attribute + selector: '[type="submit"]:not([formnovalidate])', // The disabled class disabled: '' }, @@ -2487,12 +2558,12 @@ * @param {jQuery} $field Field element * @param {Object} options Can consist of the following keys: * - message: The invalid message * @returns {Boolean} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'base64'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } return /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$/.test(value); @@ -2545,35 +2616,31 @@ * * - inclusive [optional]: Can be true or false. Default is true * - message: The invalid message * @returns {Boolean|Object} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'between'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } value = this._format(value); - if (!$.isNumeric(value)) { - return false; - } var locale = validator.getLocale(), min = $.isNumeric(options.min) ? options.min : validator.getDynamicOption($field, options.min), max = $.isNumeric(options.max) ? options.max : validator.getDynamicOption($field, options.max), minValue = this._format(min), maxValue = this._format(max); - value = parseFloat(value); return (options.inclusive === true || options.inclusive === undefined) ? { - valid: value >= minValue && value <= maxValue, + valid: $.isNumeric(value) && parseFloat(value) >= minValue && parseFloat(value) <= maxValue, message: FormValidation.Helper.format(options.message || FormValidation.I18n[locale].between['default'], [min, max]) } : { - valid: value > minValue && value < maxValue, + valid: $.isNumeric(value) && parseFloat(value) > minValue && parseFloat(value) < maxValue, message: FormValidation.Helper.format(options.message || FormValidation.I18n[locale].between.notInclusive, [min, max]) }; }, _format: function(value) { @@ -2601,12 +2668,12 @@ * @param {jQuery} $field Field element * @param {Object} options Can consist of the following keys: * - message: The invalid message * @returns {Object} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'bic'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } return /^[a-zA-Z]{6}[a-zA-Z0-9]{2}([a-zA-Z0-9]{3})?$/.test(value); } @@ -2623,22 +2690,20 @@ * (2) data is entered via the UI that is unable to be validated client-side. * (3) server returns a 400 with JSON data that contains the field that failed * validation and an associated message. * (4) ajax 400 call handler does the following: * - * bv.updateMessage(field, 'blank', errorMessage); - * bv.updateStatus(field, 'INVALID'); + * fv.updateMessage(field, 'blank', errorMessage); + * fv.updateStatus(field, 'INVALID', 'blank'); * - * @see https://github.com/formvalidation/formvalidation/issues/542 - * @see https://github.com/formvalidation/formvalidation/pull/666 * @param {FormValidation.Base} validator The validator plugin instance * @param {jQuery} $field Field element * @param {Object} options Can consist of the following keys: * - message: The invalid message * @returns {Boolean} */ - validate: function(validator, $field, options) { + validate: function(validator, $field, options, validatorName) { return true; } }; }(jQuery)); ;(function($) { @@ -2660,30 +2725,30 @@ * Return result from the callback method * * @param {FormValidation.Base} validator The validator plugin instance * @param {jQuery} $field Field element * @param {Object} options Can consist of the following keys: - * - callback: The callback method that passes 2 parameters: + * - callback: The callback method that passes parameters: * callback: function(fieldValue, validator, $field) { * // fieldValue is the value of field - * // validator is instance of BootstrapValidator + * // validator is instance of FormValidation.Base * // $field is the field element * } * - message: The invalid message * @returns {Deferred} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'callback'), + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName), dfd = new $.Deferred(), result = { valid: true }; if (options.callback) { var response = FormValidation.Helper.call(options.callback, [value, validator, $field]); result = ('boolean' === typeof response || null === response) ? { valid: response } : response; } - dfd.resolve($field, 'callback', result); + dfd.resolve($field, validatorName, result); return dfd; } }; }(jQuery)); ;(function($) { @@ -2722,11 +2787,11 @@ * - A callback function that returns the number * * - message: The invalid message * @returns {Object} */ - validate: function(validator, $field, options) { + validate: function(validator, $field, options, validatorName) { var locale = validator.getLocale(), ns = validator.getNamespace(), numChoices = $field.is('select') ? validator.getFieldElements($field.attr('data-' + ns + '-field')).find('option').filter(':selected').length : validator.getFieldElements($field.attr('data-' + ns + '-field')).filter(':checked').length, @@ -2844,12 +2909,12 @@ * @param {Object} options Can consist of the following keys: * - message: The invalid message * - type: The array of valid color types * @returns {Boolean} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'color'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } // Only accept 6 hex character values due to the HTML 5 spec @@ -2926,12 +2991,12 @@ * @param {jQuery} $field Field element * @param {Object} [options] Can consist of the following key: * - message: The invalid message * @returns {Boolean|Object} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'creditCard'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } // Accept only digits, dashes or spaces @@ -3028,23 +3093,20 @@ }); FormValidation.Validator.cusip = { /** * Validate a CUSIP number - * Examples: - * - Valid: 037833100, 931142103, 14149YAR8, 126650BG6 - * - Invalid: 31430F200, 022615AC2 * * @see http://en.wikipedia.org/wiki/CUSIP * @param {FormValidation.Base} validator The validator plugin instance * @param {jQuery} $field Field element * @param {Object} [options] Can consist of the following keys: * - message: The invalid message * @returns {Boolean} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'cusip'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } value = value.toUpperCase(); @@ -3098,15 +3160,15 @@ * @param {FormValidation.Base} validator The validator plugin instance * @param {jQuery} $field Field element * @param {Object} options Consists of the following key: * - creditCardField: The credit card number field */ - init: function(validator, $field, options) { + init: function(validator, $field, options, validatorName) { if (options.creditCardField) { var creditCardField = validator.getFieldElements(options.creditCardField); - validator.onLiveChange(creditCardField, 'live_cvv', function() { - var status = validator.getStatus($field, 'cvv'); + validator.onLiveChange(creditCardField, 'live_' + validatorName, function() { + var status = validator.getStatus($field, validatorName); if (status !== validator.STATUS_NOT_VALIDATED) { validator.revalidateField($field); } }); } @@ -3118,14 +3180,14 @@ * @param {FormValidation.Base} validator The validator plugin instance * @param {jQuery} $field Field element * @param {Object} options Consists of the following key: * - creditCardField: The credit card number field */ - destroy: function(validator, $field, options) { + destroy: function(validator, $field, options, validatorName) { if (options.creditCardField) { var creditCardField = validator.getFieldElements(options.creditCardField); - validator.offLiveChange(creditCardField, 'live_cvv'); + validator.offLiveChange(creditCardField, 'live_' + validatorName); } }, /** * Return true if the input value is a valid CVV number. @@ -3135,12 +3197,12 @@ * @param {Object} options Can consist of the following keys: * - creditCardField: The credit card number field. It can be null * - message: The invalid message * @returns {Boolean} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'cvv'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } if (!/^[0-9]{3,4}$/.test(value)) { @@ -3154,11 +3216,11 @@ // Get the credit card number var creditCard = validator.getFieldElements(options.creditCardField).val(); if (creditCard === '') { return true; } - + creditCard = creditCard.replace(/\D/g, ''); // Supported credit card types var cards = { AMERICAN_EXPRESS: { @@ -3271,12 +3333,12 @@ * ii) date and time: * The time can consist of h, m, s parts which are separated by : * ii) date, time and A (indicating AM or PM) * @returns {Boolean|Object} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'date'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } options.format = options.format || 'MM/DD/YYYY'; @@ -3464,10 +3526,11 @@ break; } return { valid: valid, + date: date, message: message }; }, /** @@ -3612,16 +3675,16 @@ * @param {FormValidation.Base} validator The validator plugin instance * @param {jQuery} $field Field element * @param {Object} options Consists of the following key: * - field: The name of field that will be used to compare with current one */ - init: function(validator, $field, options) { + init: function(validator, $field, options, validatorName) { var fields = options.field.split(','); for (var i = 0; i < fields.length; i++) { var compareWith = validator.getFieldElements(fields[i]); - validator.onLiveChange(compareWith, 'live_different', function() { - var status = validator.getStatus($field, 'different'); + validator.onLiveChange(compareWith, 'live_' + validatorName, function() { + var status = validator.getStatus($field, validatorName); if (status !== validator.STATUS_NOT_VALIDATED) { validator.revalidateField($field); } }); } @@ -3633,15 +3696,15 @@ * @param {FormValidation.Base} validator The validator plugin instance * @param {jQuery} $field Field element * @param {Object} options Consists of the following key: * - field: The name of field that will be used to compare with current one */ - destroy: function(validator, $field, options) { + destroy: function(validator, $field, options, validatorName) { var fields = options.field.split(','); for (var i = 0; i < fields.length; i++) { var compareWith = validator.getFieldElements(fields[i]); - validator.offLiveChange(compareWith, 'live_different'); + validator.offLiveChange(compareWith, 'live_' + validatorName); } }, /** * Return true if the input value is different with given field's value @@ -3651,12 +3714,12 @@ * @param {Object} options Consists of the following key: * - field: The name of field that will be used to compare with current one * - message: The invalid message * @returns {Boolean} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'different'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } var fields = options.field.split(','), @@ -3666,15 +3729,15 @@ var compareWith = validator.getFieldElements(fields[i]); if (compareWith == null || compareWith.length === 0) { continue; } - var compareValue = validator.getFieldValue(compareWith, 'different'); + var compareValue = validator.getFieldValue(compareWith, validatorName); if (value === compareValue) { isValid = false; } else if (compareValue !== '') { - validator.updateStatus(compareWith, validator.STATUS_VALID, 'different'); + validator.updateStatus(compareWith, validator.STATUS_VALID, validatorName); } } return isValid; } @@ -3696,12 +3759,12 @@ * @param {FormValidation.Base} validator Validate plugin instance * @param {jQuery} $field Field element * @param {Object} [options] * @returns {Boolean} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'digits'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } return /^\d+$/.test(value); @@ -3718,23 +3781,20 @@ }); FormValidation.Validator.ean = { /** * Validate EAN (International Article Number) - * Examples: - * - Valid: 73513537, 9780471117094, 4006381333931 - * - Invalid: 73513536 * * @see http://en.wikipedia.org/wiki/European_Article_Number * @param {FormValidation.Base} validator The validator plugin instance * @param {jQuery} $field Field element * @param {Object} options Can consist of the following keys: * - message: The invalid message * @returns {Boolean} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'ean'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } if (!/^(\d{8}|\d{12}|\d{13})$/.test(value)) { @@ -3788,12 +3848,12 @@ * @param {jQuery} $field Field element * @param {Object} options Can consist of the following keys: * - message: The invalid message * @returns {Object|Boolean} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'ein'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } if (!/^[0-9]{2}-?[0-9]{7}$/.test(value)) { @@ -3842,12 +3902,12 @@ * @param {Object} [options] * - multiple: Allow multiple email addresses, separated by a comma or semicolon; default is false. * - separator: Regex for character or characters expected as separator between addresses; default is comma /[,;]/, i.e. comma or semicolon. * @returns {Boolean} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'emailAddress'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } // Email address regular expression @@ -3942,12 +4002,12 @@ * - minTotalSize: The minimum size in bytes for all files * - message: The invalid message * - type: The allowed MIME type, separated by a comma * @returns {Boolean} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'file'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } var ext, @@ -4040,33 +4100,29 @@ * * - inclusive [optional]: Can be true or false. Default is true * - message: The invalid message * @returns {Boolean|Object} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'greaterThan'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } - + value = this._format(value); - if (!$.isNumeric(value)) { - return false; - } var locale = validator.getLocale(), compareTo = $.isNumeric(options.value) ? options.value : validator.getDynamicOption($field, options.value), compareToValue = this._format(compareTo); - value = parseFloat(value); return (options.inclusive === true || options.inclusive === undefined) ? { - valid: value >= compareToValue, + valid: $.isNumeric(value) && parseFloat(value) >= compareToValue, message: FormValidation.Helper.format(options.message || FormValidation.I18n[locale].greaterThan['default'], compareTo) } : { - valid: value > compareToValue, + valid: $.isNumeric(value) && parseFloat(value) > compareToValue, message: FormValidation.Helper.format(options.message || FormValidation.I18n[locale].greaterThan.notInclusive, compareTo) }; }, _format: function(value) { @@ -4084,23 +4140,20 @@ }); FormValidation.Validator.grid = { /** * Validate GRId (Global Release Identifier) - * Examples: - * - Valid: A12425GABC1234002M, A1-2425G-ABC1234002-M, A1 2425G ABC1234002 M, Grid:A1-2425G-ABC1234002-M - * - Invalid: A1-2425G-ABC1234002-Q * * @see http://en.wikipedia.org/wiki/Global_Release_Identifier * @param {FormValidation.Base} validator The validator plugin instance * @param {jQuery} $field Field element * @param {Object} options Can consist of the following keys: * - message: The invalid message * @returns {Boolean} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'grid'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } value = value.toUpperCase(); @@ -4132,12 +4185,12 @@ * @param {jQuery} $field Field element * @param {Object} options Consist of key: * - message: The invalid message * @returns {Boolean} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'hex'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } return /^[0-9a-fA-F]+$/.test(value); @@ -4225,22 +4278,25 @@ SE: 'Sweden', SI: 'Slovenia', SK: 'Slovakia', SM: 'San Marino', SN: 'Senegal', + TL: 'East Timor', TN: 'Tunisia', TR: 'Turkey', - VG: 'Virgin Islands, British' + VG: 'Virgin Islands, British', + XK: 'Republic of Kosovo' } } } }); FormValidation.Validator.iban = { html5Attributes: { message: 'message', - country: 'country' + country: 'country', + sepa: 'sepa' }, // http://www.swift.com/dsp/resources/documents/IBAN_Registry.pdf // http://en.wikipedia.org/wiki/International_Bank_Account_Number#IBAN_formats_by_country REGEX: { @@ -4318,15 +4374,25 @@ SE: 'SE[0-9]{2}[0-9]{3}[0-9]{16}[0-9]{1}', // Sweden SI: 'SI[0-9]{2}[0-9]{5}[0-9]{8}[0-9]{2}', // Slovenia SK: 'SK[0-9]{2}[0-9]{4}[0-9]{6}[0-9]{10}', // Slovakia SM: 'SM[0-9]{2}[A-Z]{1}[0-9]{5}[0-9]{5}[A-Z0-9]{12}', // San Marino SN: 'SN[0-9]{2}[A-Z]{1}[0-9]{23}', // Senegal + TL: 'TL38[0-9]{3}[0-9]{14}[0-9]{2}', // East Timor TN: 'TN59[0-9]{2}[0-9]{3}[0-9]{13}[0-9]{2}', // Tunisia TR: 'TR[0-9]{2}[0-9]{5}[A-Z0-9]{1}[A-Z0-9]{16}', // Turkey - VG: 'VG[0-9]{2}[A-Z]{4}[0-9]{16}' // Virgin Islands, British + VG: 'VG[0-9]{2}[A-Z]{4}[0-9]{16}', // Virgin Islands, British + XK: 'XK[0-9]{2}[0-9]{4}[0-9]{10}[0-9]{2}' // Republic of Kosovo }, + // List of SEPA country codes + SEPA_COUNTRIES: [ + 'AT', 'BE', 'BG', 'CH', 'CY', 'CZ', 'DE', 'DK', 'EE', 'ES', + 'FI', 'FR', 'GB', 'GI', 'GR', 'HR', 'HU', 'IE', 'IS', 'IT', + 'LI', 'LT', 'LU', 'LV', 'MC', 'MT', 'NL', 'NO', 'PL', 'PT', + 'RO', 'SE', 'SI', 'SK', 'SM' + ], + /** * Validate an International Bank Account Number (IBAN) * To test it, take the sample IBAN from * http://www.nordea.com/Our+services/International+products+and+services/Cash+Management/IBAN+countries/908462.html * @@ -4337,14 +4403,16 @@ * - country: The ISO 3166-1 country code. It can be * - A country code * - Name of field which its value defines the country code * - Name of callback function that returns the country code * - A callback function that returns the country code + * - sepa: Set it to true (false) to indicate that the IBAN number must be (not be) from SEPA countries + * By default, this option is not defined * @returns {Boolean|Object} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'iban'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } value = value.replace(/[^a-zA-Z0-9]/g, '').toUpperCase(); @@ -4356,13 +4424,23 @@ country = validator.getDynamicOption($field, country); } var locale = validator.getLocale(); if (!this.REGEX[country]) { - return true; + return false; } + // Check whether or not the sepa option is enabled + if (typeof options.sepa !== undefined) { + var isSepaCountry = $.inArray(country, this.SEPA_COUNTRIES) !== -1; + if (((options.sepa === 'true' || options.sepa === true) && !isSepaCountry) + || ((options.sepa === 'false' || options.sepa === false) && isSepaCountry)) + { + return false; + } + } + if (!(new RegExp('^' + this.REGEX[country] + '$')).test(value)) { return { valid: false, message: FormValidation.Helper.format(options.message || FormValidation.I18n[locale].iban.country, FormValidation.I18n[locale].iban.countries[country]) }; @@ -4454,14 +4532,14 @@ * - country: The ISO 3166-1 country code. It can be * - One of country code defined in COUNTRY_CODES * - Name of field which its value defines the country code * - Name of callback function that returns the country code * - A callback function that returns the country code - * @returns {Boolean|Object} + * @returns {Object} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'id'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } var locale = validator.getLocale(), @@ -4475,17 +4553,16 @@ if ($.inArray(country, this.COUNTRY_CODES) === -1) { return true; } - var method = ['_', country.toLowerCase()].join(''); - return this[method](value) - ? true - : { - valid: false, - message: FormValidation.Helper.format(options.message || FormValidation.I18n[locale].id.country, FormValidation.I18n[locale].id.countries[country.toUpperCase()]) - }; + var method = ['_', country.toLowerCase()].join(''), + result = this[method](value); + result = (result === true || result === false) ? { valid: result } : result; + result.message = FormValidation.Helper.format(options.message || FormValidation.I18n[locale].id.country, FormValidation.I18n[locale].id.countries[country.toUpperCase()]); + + return result; }, /** * Validate Unique Master Citizen Number which uses in * - Bosnia and Herzegovina (country code: BA) @@ -4574,13 +4651,10 @@ return this._validateJMBG(value, 'SI'); }, /** * Validate Bulgarian national identification number (EGN) - * Examples: - * - Valid: 7523169263, 8032056031, 803205 603 1, 8001010008, 7501020018, 7552010005, 7542011030 - * - Invalid: 8019010008 * * @see http://en.wikipedia.org/wiki/Uniform_civil_number * @param {String} value The ID * @returns {Boolean} */ @@ -4614,13 +4688,10 @@ return (sum + '' === value.substr(9, 1)); }, /** * Validate Brazilian national identification number (CPF) - * Examples: - * - Valid: 39053344705, 390.533.447-05, 111.444.777-35 - * - Invalid: 231.002.999-00 * * @see http://en.wikipedia.org/wiki/Cadastro_de_Pessoas_F%C3%ADsicas * @param {String} value The ID * @returns {Boolean} */ @@ -4655,12 +4726,10 @@ return (d2 + '' === value.charAt(10)); }, /** * Validate Swiss Social Security Number (AHV-Nr/No AVS) - * Examples: - * - Valid: 756.1234.5678.95, 7561234567895 * * @see http://en.wikipedia.org/wiki/National_identification_number#Switzerland * @see http://www.bsv.admin.ch/themen/ahv/00011/02185/index.html?lang=de * @param {String} value The ID * @returns {Boolean} @@ -4680,12 +4749,10 @@ return (sum + '' === value.charAt(length - 1)); }, /** * Validate Chilean national identification number (RUN/RUT) - * Examples: - * - Valid: 76086428-5, 22060449-7, 12531909-2 * * @see http://en.wikipedia.org/wiki/National_identification_number#Chile * @see https://palena.sii.cl/cvc/dte/ee_empresas_emisoras.html for samples * @param {String} value The ID * @returns {Boolean} @@ -4740,11 +4807,11 @@ // Basic format check (18 or 15 digits, considering X in checksum) value = value.trim(); if (!/^\d{15}$/.test(value) && !/^\d{17}[\dXx]{1}$/.test(value)) { return false; } - + // Check China PR Administrative division code var adminDivisionCodes = { 11: { 0: [0], 1: [[0, 9], [11, 17]], @@ -5194,15 +5261,15 @@ }, 71: { 0: [0] }, 81: { 0: [0] }, 82: { 0: [0] } }; - + var provincial = parseInt(value.substr(0, 2), 10), prefectural = parseInt(value.substr(2, 2), 10), county = parseInt(value.substr(4, 2), 10); - + if (!adminDivisionCodes[provincial] || !adminDivisionCodes[provincial][prefectural]) { return false; } var inRange = false, rangeDef = adminDivisionCodes[provincial][prefectural]; @@ -5216,25 +5283,25 @@ } if (!inRange) { return false; } - + // Check date of birth var dob; if (value.length === 18) { dob = value.substr(6, 8); - } else /* length == 15 */ { + } else /* length == 15 */ { dob = '19' + value.substr(6, 6); } var year = parseInt(dob.substr(0, 4), 10), month = parseInt(dob.substr(4, 2), 10), day = parseInt(dob.substr(6, 2), 10); if (!FormValidation.Helper.date(year, month, day)) { return false; } - + // Check checksum (18-digit system only) if (value.length === 18) { var sum = 0, weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; for (i = 0; i < 17; i++) { @@ -5242,19 +5309,16 @@ } sum = (12 - (sum % 11)) % 11; var checksum = (value.charAt(17).toUpperCase() !== 'X') ? parseInt(value.charAt(17), 10) : 10; return checksum === sum; } - + return true; }, - + /** * Validate Czech national identification number (RC) - * Examples: - * - Valid: 7103192745, 991231123 - * - Invalid: 1103492745, 590312123 * * @param {String} value The ID * @returns {Boolean} */ _cz: function(value) { @@ -5291,13 +5355,10 @@ return true; }, /** * Validate Danish Personal Identification number (CPR) - * Examples: - * - Valid: 2110625629, 211062-5629 - * - Invalid: 511062-5629 * * @see https://en.wikipedia.org/wiki/Personal_identification_number_(Denmark) * @param {String} value The ID * @returns {Boolean} */ @@ -5326,12 +5387,10 @@ return FormValidation.Helper.date(year, month, day); }, /** * Validate Estonian Personal Identification Code (isikukood) - * Examples: - * - Valid: 37605030299 * * @see http://et.wikipedia.org/wiki/Isikukood * @param {String} value The ID * @returns {Boolean} */ @@ -5340,49 +5399,44 @@ return this._lt(value); }, /** * Validate Spanish personal identity code (DNI) - * Support i) DNI (for Spanish citizens), ii) NIE (for foreign people) - * and iii) CIF (for legal entities) + * Support DNI (for Spanish citizens), NIE (for foreign people) and CIF (for legal entities) * - * Examples: - * - Valid: - * i) 54362315K, 54362315-K - * ii) X2482300W, X-2482300W, X-2482300-W - * iii) A58818501, A-58818501 - * - Invalid: - * i) 54362315Z - * ii) X-2482300A - * iii) K58818501, G58818507 - * * @see https://en.wikipedia.org/wiki/National_identification_number#Spain * @param {String} value The ID - * @returns {Boolean} + * @returns {Boolean|Object} */ _es: function(value) { var isDNI = /^[0-9]{8}[-]{0,1}[A-HJ-NP-TV-Z]$/.test(value), isNIE = /^[XYZ][-]{0,1}[0-9]{7}[-]{0,1}[A-HJ-NP-TV-Z]$/.test(value), isCIF = /^[A-HNPQS][-]{0,1}[0-9]{7}[-]{0,1}[0-9A-J]$/.test(value); if (!isDNI && !isNIE && !isCIF) { return false; } value = value.replace(/-/g, ''); - var check; + var check, type, isValid = true; if (isDNI || isNIE) { + type = 'DNI'; var index = 'XYZ'.indexOf(value.charAt(0)); if (index !== -1) { // It is NIE number value = index + value.substr(1) + ''; + type = 'NIE'; } check = parseInt(value.substr(0, 8), 10); check = 'TRWAGMYFPDXBNJZSQVHLCKE'[check % 23]; - return (check === value.substr(8, 1)); + return { + valid: (check === value.substr(8, 1)), + type: type + }; } else { check = value.substr(1, 7); + type = 'CIF'; var letter = value[0], control = value.substr(-1), sum = 0; // The digits in the even positions are added to the sum directly. @@ -5405,29 +5459,31 @@ // If that last digit is not 0, subtract it from 10 var lastDigit = sum - (Math.floor(sum / 10) * 10); if (lastDigit !== 0) { lastDigit = 10 - lastDigit; } - + if ('KQS'.indexOf(letter) !== -1) { // If the CIF starts with a K, Q or S, the control digit must be a letter - return (control === 'JABCDEFGHI'[lastDigit]); + isValid = (control === 'JABCDEFGHI'[lastDigit]); } else if ('ABEH'.indexOf(letter) !== -1) { // If it starts with A, B, E or H, it has to be a number - return (control === ('' + lastDigit)); + isValid = (control === ('' + lastDigit)); } else { // In any other case, it doesn't matter - return (control === ('' + lastDigit) || control === 'JABCDEFGHI'[lastDigit]); + isValid = (control === ('' + lastDigit) || control === 'JABCDEFGHI'[lastDigit]); } + + return { + valid: isValid, + type: type + }; } }, /** * Validate Finnish Personal Identity Code (HETU) - * Examples: - * - Valid: 311280-888Y, 131052-308T - * - Invalid: 131052-308U, 310252-308Y * * @param {String} value The ID * @returns {Boolean} */ _fi: function(value) { @@ -5457,13 +5513,10 @@ return '0123456789ABCDEFHJKLMNPRSTUVWXY'.charAt(n % 31) === value.charAt(10); }, /** * Validate Croatian personal identification number (OIB) - * Examples: - * - Valid: 33392005961 - * - Invalid: 33392005962 * * @param {String} value The ID * @returns {Boolean} */ _hr: function(value) { @@ -5473,13 +5526,10 @@ return FormValidation.Helper.mod11And10(value); }, /** * Validate Irish Personal Public Service Number (PPS) - * Examples: - * - Valid: 6433435F, 6433435FT, 6433435FW, 6433435OA, 6433435IH, 1234567TW, 1234567FA - * - Invalid: 6433435E, 6433435VH * * @see https://en.wikipedia.org/wiki/Personal_Public_Service_Number * @param {String} value The ID * @returns {Boolean} */ @@ -5511,14 +5561,12 @@ } }, /** * Validate Iceland national identification number (Kennitala) - * Examples: - * - Valid: 120174-3399, 1201743399, 0902862349 * - * @see http://en.wikipedia.org/wiki/Kennitala + * @see http://en.wikipedia.org/wiki/Kennitala * @param {String} value The ID * @returns {Boolean} */ _is: function(value) { if (!/^[0-9]{6}[-]{0,1}[0-9]{4}$/.test(value)) { @@ -5544,13 +5592,10 @@ return (sum + '' === value.charAt(8)); }, /** * Validate Lithuanian Personal Code (Asmens kodas) - * Examples: - * - Valid: 38703181745 - * - Invalid: 38703181746, 78703181745, 38703421745 * * @see http://en.wikipedia.org/wiki/National_identification_number#Lithuania * @see http://www.adomas.org/midi2007/pcode.html * @param {String} value The ID * @returns {Boolean} @@ -5593,13 +5638,10 @@ return (sum + '' === value.charAt(10)); }, /** * Validate Latvian Personal Code (Personas kods) - * Examples: - * - Valid: 161175-19997, 16117519997 - * - Invalid: 161375-19997 * * @see http://laacz.lv/2006/11/25/pk-parbaudes-algoritms/ * @param {String} value The ID * @returns {Boolean} */ @@ -5628,20 +5670,21 @@ return (sum + '' === value.charAt(10)); }, /** * Validate Dutch national identification number (BSN) - * Examples: - * - Valid: 111222333, 941331490, 9413.31.490 - * - Invalid: 111252333 * * @see https://nl.wikipedia.org/wiki/Burgerservicenummer * @param {String} value The ID * @returns {Boolean} */ _nl: function(value) { - while (value.length < 9) { + if (value.length < 8) { + return false; + } + + if (value.length === 8) { value = '0' + value; } if (!/^[0-9]{4}[.]{0,1}[0-9]{2}[.]{0,1}[0-9]{3}$/.test(value)) { return false; } @@ -5658,14 +5701,14 @@ if (sum === 10) { sum = 0; } return (sum + '' === value.charAt(length - 1)); }, - + /** * Validate Poland citizen number (PESEL) - * + * * @see http://en.wikipedia.org/wiki/National_identification_number#Poland * @see http://en.wikipedia.org/wiki/PESEL * @param {String} value The ID * @returns {Boolean} */ @@ -5690,13 +5733,10 @@ return (sum + '' === value.charAt(length - 1)); }, /** * Validate Romanian numerical personal code (CNP) - * Examples: - * - Valid: 1630615123457, 1800101221144 - * - Invalid: 8800101221144, 1632215123457, 1630615123458 * * @see http://en.wikipedia.org/wiki/National_identification_number#Romania * @param {String} value The ID * @returns {Boolean} */ @@ -5746,13 +5786,10 @@ return (sum + '' === value.charAt(length - 1)); }, /** * Validate Swedish personal identity number (personnummer) - * Examples: - * - Valid: 8112289874, 811228-9874, 811228+9874 - * - Invalid: 811228-9873 * * @see http://en.wikipedia.org/wiki/Personal_identity_number_(Sweden) * @param {String} value The ID * @returns {Boolean} */ @@ -5773,13 +5810,10 @@ return FormValidation.Helper.luhn(value); }, /** * Validate Slovak national identifier number (RC) - * Examples: - * - Valid: 7103192745, 991231123 - * - Invalid: 7103192746, 1103492745 * * @param {String} value The ID * @returns {Boolean} */ _sk: function(value) { @@ -5798,13 +5832,10 @@ return /^\d{5}$/.test(value); }, /** * Validate Thailand citizen number - * Examples: - * - Valid: 7145620509547, 3688699975685, 2368719339716 - * - Invalid: 1100800092310 * * @see http://en.wikipedia.org/wiki/National_identification_number#Thailand * @param {String} value The ID * @returns {Boolean} */ @@ -5821,13 +5852,10 @@ return (11 - sum % 11) % 10 === parseInt(value.charAt(12), 10); }, /** * Validate South African ID - * Example: - * - Valid: 8001015009087 - * - Invalid: 8001015009287, 8001015009086 * * @see http://en.wikipedia.org/wiki/National_identification_number#South_Africa * @param {String} value The ID * @returns {Boolean} */ @@ -5871,14 +5899,14 @@ * @param {FormValidation.Base} validator The validator plugin instance * @param {jQuery} $field Field element * @param {Object} options Consists of the following key: * - field: The name of field that will be used to compare with current one */ - init: function(validator, $field, options) { + init: function(validator, $field, options, validatorName) { var compareWith = validator.getFieldElements(options.field); - validator.onLiveChange(compareWith, 'live_identical', function() { - var status = validator.getStatus($field, 'identical'); + validator.onLiveChange(compareWith, 'live_' + validatorName, function() { + var status = validator.getStatus($field, validatorName); if (status !== validator.STATUS_NOT_VALIDATED) { validator.revalidateField($field); } }); }, @@ -5889,13 +5917,13 @@ * @param {FormValidation.Base} validator The validator plugin instance * @param {jQuery} $field Field element * @param {Object} options Consists of the following key: * - field: The name of field that will be used to compare with current one */ - destroy: function(validator, $field, options) { + destroy: function(validator, $field, options, validatorName) { var compareWith = validator.getFieldElements(options.field); - validator.offLiveChange(compareWith, 'live_identical'); + validator.offLiveChange(compareWith, 'live_' + validatorName); }, /** * Check if input value equals to value of particular one * @@ -5903,20 +5931,20 @@ * @param {jQuery} $field Field element * @param {Object} options Consists of the following key: * - field: The name of field that will be used to compare with current one * @returns {Boolean} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'identical'), + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName), compareWith = validator.getFieldElements(options.field); if (compareWith === null || compareWith.length === 0) { return true; } - var compareValue = validator.getFieldValue(compareWith, 'identical'); + var compareValue = validator.getFieldValue(compareWith, validatorName); if (value === compareValue) { - validator.updateStatus(compareWith, validator.STATUS_VALID, 'identical'); + validator.updateStatus(compareWith, validator.STATUS_VALID, validatorName); return true; } return false; } @@ -5932,23 +5960,20 @@ }); FormValidation.Validator.imei = { /** * Validate IMEI (International Mobile Station Equipment Identity) - * Examples: - * - Valid: 35-209900-176148-1, 35-209900-176148-23, 3568680000414120, 490154203237518 - * - Invalid: 490154203237517 * * @see http://en.wikipedia.org/wiki/International_Mobile_Station_Equipment_Identity * @param {FormValidation.Base} validator The validator plugin instance * @param {jQuery} $field Field element * @param {Object} options Can consist of the following keys: * - message: The invalid message * @returns {Boolean} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'imei'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } switch (true) { @@ -5980,39 +6005,31 @@ }); FormValidation.Validator.imo = { /** * Validate IMO (International Maritime Organization) - * Examples: - * - Valid: IMO 8814275, IMO 9176187 - * - Invalid: IMO 8814274 * * @see http://en.wikipedia.org/wiki/IMO_Number * @param {FormValidation.Base} validator The validator plugin instance * @param {jQuery} $field Field element * @param {Object} options Can consist of the following keys: * - message: The invalid message * @returns {Boolean} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'imo'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } if (!/^IMO \d{7}$/i.test(value)) { return false; } - + // Grab just the digits var sum = 0, digits = value.replace(/^.*(\d{7})$/, '$1'); - - // Go over each char, multiplying by the inverse of it's position - // IMO 9176187 - // (9 * 7) + (1 * 6) + (7 * 5) + (6 * 4) + (1 * 3) + (8 * 2) = 147 - // Take the last digit of that, that's the check digit (7) for (var i = 6; i >= 1; i--) { sum += (digits.slice((6 - i), -i) * (i + 1)); } return sum % 10 === parseInt(digits.charAt(6), 10); @@ -6027,10 +6044,16 @@ } } }); FormValidation.Validator.integer = { + html5Attributes: { + message: 'message', + thousandsseparator: 'thousandsSeparator', + decimalseparator: 'decimalSeparator' + }, + enableByHtml5: function($field) { return ('number' === $field.attr('type')) && ($field.attr('step') === undefined || $field.attr('step') % 1 === 0); }, /** @@ -6038,22 +6061,51 @@ * * @param {FormValidation.Base} validator The validator plugin instance * @param {jQuery} $field Field element * @param {Object} options Can consist of the following key: * - message: The invalid message + * - thousandsSeparator: The thousands separator. It's empty by default + * - decimalSeparator: The decimal separator. It's '.' by default * @returns {Boolean} */ - validate: function(validator, $field, options) { + validate: function(validator, $field, options, validatorName) { if (this.enableByHtml5($field) && $field.get(0).validity && $field.get(0).validity.badInput === true) { return false; } - var value = validator.getFieldValue($field, 'integer'); + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } - return /^(?:-?(?:0|[1-9][0-9]*))$/.test(value); + + var decimalSeparator = options.decimalSeparator || '.', + thousandsSeparator = options.thousandsSeparator || ''; + decimalSeparator = (decimalSeparator === '.') ? '\\.' : decimalSeparator; + thousandsSeparator = (thousandsSeparator === '.') ? '\\.' : thousandsSeparator; + + var testRegexp = new RegExp('^-?[0-9]{1,3}(' + thousandsSeparator + '[0-9]{3})*(' + decimalSeparator + '[0-9]+)?$'), + thousandsReplacer = new RegExp(thousandsSeparator, 'g'); + + if (!testRegexp.test(value)) { + return false; + } + + // Replace thousands separator with blank + if (thousandsSeparator) { + value = value.replace(thousandsReplacer, ''); + } + // Replace decimal separator with a dot + if (decimalSeparator) { + value = value.replace(decimalSeparator, '.'); + } + + if (isNaN(value) || !isFinite(value)) { + return false; + } + // TODO: Use Number.isInteger() if available + value = parseFloat(value); + return Math.floor(value) === value; } }; }(jQuery)); ;(function($) { FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, { @@ -6082,12 +6134,12 @@ * - ipv4: Enable IPv4 validator, default to true * - ipv6: Enable IPv6 validator, default to true * - message: The invalid message * @returns {Boolean|Object} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'ip'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } options = $.extend({}, { ipv4: true, ipv6: true }, options); @@ -6133,27 +6185,20 @@ }); FormValidation.Validator.isbn = { /** * Return true if the input value is a valid ISBN 10 or ISBN 13 number - * Examples: - * - Valid: - * ISBN 10: 99921-58-10-7, 9971-5-0210-0, 960-425-059-0, 80-902734-1-6, 85-359-0277-5, 1-84356-028-3, 0-684-84328-5, 0-8044-2957-X, 0-85131-041-9, 0-943396-04-2, 0-9752298-0-X - * ISBN 13: 978-0-306-40615-7 - * - Invalid: - * ISBN 10: 99921-58-10-6 - * ISBN 13: 978-0-306-40615-6 * * @see http://en.wikipedia.org/wiki/International_Standard_Book_Number * @param {FormValidation.Base} validator The validator plugin instance * @param {jQuery} $field Field element * @param {Object} [options] Can consist of the following keys: * - message: The invalid message - * @returns {Boolean} + * @returns {Boolean|Object} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'isbn'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } // http://en.wikipedia.org/wiki/International_Standard_Book_Number#Overview @@ -6192,22 +6237,28 @@ if (checksum === 11) { checksum = 0; } else if (checksum === 10) { checksum = 'X'; } - return (checksum + '' === chars[length - 1]); + return { + type: type, + valid: (checksum + '' === chars[length - 1]) + }; case 'ISBN13': sum = 0; for (i = 0; i < length - 1; i++) { sum += ((i % 2 === 0) ? parseInt(chars[i], 10) : (parseInt(chars[i], 10) * 3)); } checksum = 10 - (sum % 10); if (checksum === 10) { checksum = '0'; } - return (checksum + '' === chars[length - 1]); + return { + type: type, + valid: (checksum + '' === chars[length - 1]) + }; default: return false; } } @@ -6227,23 +6278,20 @@ // See http://isin.net/country-codes/ COUNTRY_CODES: 'AF|AX|AL|DZ|AS|AD|AO|AI|AQ|AG|AR|AM|AW|AU|AT|AZ|BS|BH|BD|BB|BY|BE|BZ|BJ|BM|BT|BO|BQ|BA|BW|BV|BR|IO|BN|BG|BF|BI|KH|CM|CA|CV|KY|CF|TD|CL|CN|CX|CC|CO|KM|CG|CD|CK|CR|CI|HR|CU|CW|CY|CZ|DK|DJ|DM|DO|EC|EG|SV|GQ|ER|EE|ET|FK|FO|FJ|FI|FR|GF|PF|TF|GA|GM|GE|DE|GH|GI|GR|GL|GD|GP|GU|GT|GG|GN|GW|GY|HT|HM|VA|HN|HK|HU|IS|IN|ID|IR|IQ|IE|IM|IL|IT|JM|JP|JE|JO|KZ|KE|KI|KP|KR|KW|KG|LA|LV|LB|LS|LR|LY|LI|LT|LU|MO|MK|MG|MW|MY|MV|ML|MT|MH|MQ|MR|MU|YT|MX|FM|MD|MC|MN|ME|MS|MA|MZ|MM|NA|NR|NP|NL|NC|NZ|NI|NE|NG|NU|NF|MP|NO|OM|PK|PW|PS|PA|PG|PY|PE|PH|PN|PL|PT|PR|QA|RE|RO|RU|RW|BL|SH|KN|LC|MF|PM|VC|WS|SM|ST|SA|SN|RS|SC|SL|SG|SX|SK|SI|SB|SO|ZA|GS|SS|ES|LK|SD|SR|SJ|SZ|SE|CH|SY|TW|TJ|TZ|TH|TL|TG|TK|TO|TT|TN|TR|TM|TC|TV|UG|UA|AE|GB|US|UM|UY|UZ|VU|VE|VN|VG|VI|WF|EH|YE|ZM|ZW', /** * Validate an ISIN (International Securities Identification Number) - * Examples: - * - Valid: US0378331005, AU0000XVGZA3, GB0002634946 - * - Invalid: US0378331004, AA0000XVGZA3 * * @see http://en.wikipedia.org/wiki/International_Securities_Identifying_Number * @param {FormValidation.Base} validator The validator plugin instance * @param {jQuery} $field Field element * @param {Object} options Can consist of the following keys: * - message: The invalid message * @returns {Boolean} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'isin'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } value = value.toUpperCase(); @@ -6286,23 +6334,20 @@ }); FormValidation.Validator.ismn = { /** * Validate ISMN (International Standard Music Number) - * Examples: - * - Valid: M230671187, 979-0-0601-1561-5, 979 0 3452 4680 5, 9790060115615 - * - Invalid: 9790060115614 * * @see http://en.wikipedia.org/wiki/International_Standard_Music_Number * @param {FormValidation.Base} validator The validator plugin instance * @param {jQuery} $field Field element * @param {Object} options Can consist of the following keys: * - message: The invalid message - * @returns {Boolean} + * @returns {Boolean|Object} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'ismn'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } // Groups are separated by a hyphen or a space @@ -6333,11 +6378,14 @@ weight = [1, 3]; for (var i = 0; i < length - 1; i++) { sum += parseInt(value.charAt(i), 10) * weight[i % 2]; } sum = 10 - sum % 10; - return (sum + '' === value.charAt(length - 1)); + return { + type: type, + valid: (sum + '' === value.charAt(length - 1)) + }; } }; }(jQuery)); ;(function($) { FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, { @@ -6349,23 +6397,20 @@ }); FormValidation.Validator.issn = { /** * Validate ISSN (International Standard Serial Number) - * Examples: - * - Valid: 0378-5955, 0024-9319, 0032-1478 - * - Invalid: 0032-147X * * @see http://en.wikipedia.org/wiki/International_Standard_Serial_Number * @param {FormValidation.Base} validator The validator plugin instance * @param {jQuery} $field Field element * @param {Object} options Can consist of the following keys: * - message: The invalid message * @returns {Boolean} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'issn'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } // Groups are separated by a hyphen or a space @@ -6432,33 +6477,29 @@ * * - inclusive [optional]: Can be true or false. Default is true * - message: The invalid message * @returns {Boolean|Object} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'lessThan'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } - + value = this._format(value); - if (!$.isNumeric(value)) { - return false; - } var locale = validator.getLocale(), compareTo = $.isNumeric(options.value) ? options.value : validator.getDynamicOption($field, options.value), compareToValue = this._format(compareTo); - value = parseFloat(value); return (options.inclusive === true || options.inclusive === undefined) ? { - valid: value <= compareToValue, + valid: $.isNumeric(value) && parseFloat(value) <= compareToValue, message: FormValidation.Helper.format(options.message || FormValidation.I18n[locale].lessThan['default'], compareTo) } : { - valid: value < compareToValue, + valid: $.isNumeric(value) && parseFloat(value) < compareToValue, message: FormValidation.Helper.format(options.message || FormValidation.I18n[locale].lessThan.notInclusive, compareTo) }; }, _format: function(value) { @@ -6483,12 +6524,12 @@ * @param {jQuery} $field Field element * @param {Object} options Can consist of the following keys: * - message: The invalid message * @returns {Boolean} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'mac'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } return /^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$/.test(value); @@ -6505,23 +6546,20 @@ }); FormValidation.Validator.meid = { /** * Validate MEID (Mobile Equipment Identifier) - * Examples: - * - Valid: 293608736500703710, 29360-87365-0070-3710, AF0123450ABCDE, AF-012345-0ABCDE - * - Invalid: 2936087365007037101 * * @see http://en.wikipedia.org/wiki/Mobile_equipment_identifier * @param {FormValidation.Base} validator The validator plugin instance * @param {jQuery} $field Field element * @param {Object} options Can consist of the following keys: * - message: The invalid message * @returns {Boolean} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'meid'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } switch (true) { @@ -6603,11 +6641,11 @@ * @param {FormValidation.Base} validator The validator plugin instance * @param {jQuery} $field Field element * @param {Object} options * @returns {Boolean} */ - validate: function(validator, $field, options) { + validate: function(validator, $field, options, validatorName) { var type = $field.attr('type'); if ('radio' === type || 'checkbox' === type) { var ns = validator.getNamespace(); return validator .getFieldElements($field.attr('data-' + ns + '-field')) @@ -6617,11 +6655,11 @@ if ('number' === type && $field.get(0).validity && $field.get(0).validity.badInput === true) { return true; } - var value = validator.getFieldValue($field, 'notEmpty'); + var value = validator.getFieldValue($field, validatorName); return $.trim(value) !== ''; } }; }(jQuery)); ;(function($) { @@ -6634,11 +6672,13 @@ }); FormValidation.Validator.numeric = { html5Attributes: { message: 'message', - separator: 'separator' + separator: 'separator', // deprecated + thousandsseparator: 'thousandsSeparator', + decimalseparator: 'decimalSeparator' }, enableByHtml5: function($field) { return ('number' === $field.attr('type')) && ($field.attr('step') !== undefined) && ($field.attr('step') % 1 !== 0); }, @@ -6648,27 +6688,45 @@ * * @param {FormValidation.Base} validator The validator plugin instance * @param {jQuery} $field Field element * @param {Object} options Consist of key: * - message: The invalid message - * - separator: The decimal separator. Can be "." (default), "," + * - thousandsSeparator: The thousands separator. It's empty by default + * - separator, decimalSeparator: The decimal separator. It's '.' by default + * The separator option is deprecated and should be replaced with decimalSeparator * @returns {Boolean} */ - validate: function(validator, $field, options) { + validate: function(validator, $field, options, validatorName) { if (this.enableByHtml5($field) && $field.get(0).validity && $field.get(0).validity.badInput === true) { return false; } - var value = validator.getFieldValue($field, 'numeric'); + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } - var separator = options.separator || '.'; - if (separator !== '.') { - value = value.replace(separator, '.'); + var decimalSeparator = options.separator || options.decimalSeparator || '.', + thousandsSeparator = options.thousandsSeparator || ''; + decimalSeparator = (decimalSeparator === '.') ? '\\.' : decimalSeparator; + thousandsSeparator = (thousandsSeparator === '.') ? '\\.' : thousandsSeparator; + + var testRegexp = new RegExp('^-?[0-9]{1,3}(' + thousandsSeparator + '[0-9]{3})*(' + decimalSeparator + '[0-9]+)?$'), + thousandsReplacer = new RegExp(thousandsSeparator, 'g'); + + if (!testRegexp.test(value)) { + return false; } + // Replace thousands separator with blank + if (thousandsSeparator) { + value = value.replace(thousandsReplacer, ''); + } + // Replace decimal separator with a dot + if (decimalSeparator) { + value = value.replace(decimalSeparator, '.'); + } + return !isNaN(parseFloat(value)) && isFinite(value); } }; }(jQuery)); ;(function($) { @@ -6726,12 +6784,12 @@ * - Name of callback function that returns the country code * - A callback function that returns the country code * * @returns {Boolean|Object} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'phone'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } var locale = validator.getLocale(), @@ -6750,11 +6808,11 @@ case 'AE': // http://regexr.com/39tak value = $.trim(value); isValid = (/^(((\+|00)?971[\s\.-]?(\(0\)[\s\.-]?)?|0)(\(5(0|2|5|6)\)|5(0|2|5|6)|2|3|4|6|7|9)|60)([\s\.-]?[0-9]){7}$/).test(value); break; - + case 'BG': // https://regex101.com/r/yE6vN4/1 // See http://en.wikipedia.org/wiki/Telephone_numbers_in_Bulgaria value = value.replace(/\+|\s|-|\/|\(|\)/gi,''); isValid = (/^(0|359|00)(((700|900)[0-9]{5}|((800)[0-9]{5}|(800)[0-9]{4}))|(87|88|89)([0-9]{7})|((2[0-9]{7})|(([3-9][0-9])(([0-9]{6})|([0-9]{5})))))$/).test(value); @@ -6815,33 +6873,33 @@ // http://aa-asterisk.org.uk/index.php/Regular_Expressions_for_Validating_and_Formatting_GB_Telephone_Numbers#Match_GB_telephone_number_in_any_format // http://regexr.com/38uhv value = $.trim(value); isValid = (/^\(?(?:(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?\(?(?:0\)?[\s-]?\(?)?|0)(?:\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}|\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4}|\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3})|\d{5}\)?[\s-]?\d{4,5}|8(?:00[\s-]?11[\s-]?11|45[\s-]?46[\s-]?4\d))(?:(?:[\s-]?(?:x|ext\.?\s?|\#)\d+)?)$/).test(value); break; - + case 'IN': // http://stackoverflow.com/questions/18351553/regular-expression-validation-for-indian-phone-number-and-mobile-number // http://regex101.com/r/qL6eZ5/1 // May begin with +91. Supports mobile and land line numbers value = $.trim(value); isValid = (/((\+?)((0[ -]+)*|(91 )*)(\d{12}|\d{10}))|\d{5}([- ]*)\d{6}/).test(value); break; - + case 'MA': // http://en.wikipedia.org/wiki/Telephone_numbers_in_Morocco // http://regexr.com/399n8 value = $.trim(value); isValid = (/^(?:(?:(?:\+|00)212[\s]?(?:[\s]?\(0\)[\s]?)?)|0){1}(?:5[\s.-]?[2-3]|6[\s.-]?[13-9]){1}[0-9]{1}(?:[\s.-]?\d{2}){3}$/).test(value); break; - + case 'NL': // http://en.wikipedia.org/wiki/Telephone_numbers_in_the_Netherlands // http://regexr.com/3aevr value = $.trim(value); isValid = (/^((\+|00(\s|\s?\-\s?)?)31(\s|\s?\-\s?)?(\(0\)[\-\s]?)?|0)[1-9]((\s|\s?\-\s?)?[0-9])((\s|\s?-\s?)?[0-9])((\s|\s?-\s?)?[0-9])\s?[0-9]\s?[0-9]\s?[0-9]\s?[0-9]\s?[0-9]$/gm).test(value); break; - + case 'PK': // http://regex101.com/r/yH8aV9/2 value = $.trim(value); isValid = (/^0?3[0-9]{2}[0-9]{7}$/).test(value); break; @@ -6870,11 +6928,11 @@ case 'VE': // http://regex101.com/r/eM2yY0/6 value = $.trim(value); isValid = (/^0(?:2(?:12|4[0-9]|5[1-9]|6[0-9]|7[0-8]|8[1-35-8]|9[1-5]|3[45789])|4(?:1[246]|2[46]))\d{7}$/).test(value); break; - + case 'US': /* falls through */ default: // Make sure US phone numbers have 10 digits // May start with 1, +1, or 1-; should discard @@ -6892,10 +6950,78 @@ }; }(jQuery)); ;(function($) { FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, { 'en_US': { + promise: { + 'default': 'Please enter a valid value' + } + } + }); + + FormValidation.Validator.promise = { + html5Attributes: { + message: 'message', + promise: 'promise' + }, + + /** + * Return result from a jQuery's Deferred object + * + * @param {FormValidation.Base} validator The validator plugin instance + * @param {jQuery} $field Field element + * @param {Object} options Can consist of the following keys: + * - promise: The method that passes parameters: + * promise: function(fieldValue, validator, $field) { + * // fieldValue is the value of field + * // validator is instance of FormValidation.Base + * // $field is the field element + * + * var dfd = new $.Deferred(); + * + * // Do something ... + * + * // Resolve when particular task is done + * dfd.resolve({ + * valid: true or false, // Required + * message: 'Other message', // Optional + * key: value // You can attach more data to reuse later + * }); + * + * // You can reject if there's error + * dfd.reject({ + * message: 'Other message', // Optional + * key: value // You can attach more data to reuse later + * }); + * + * return dfd.promise(); + * } + * - message: The invalid message + * @returns {Deferred} + */ + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName), + dfd = new $.Deferred(), + promise = FormValidation.Helper.call(options.promise, [value, validator, $field]); + + promise + .done(function(result) { + dfd.resolve($field, validatorName, result); + }) + .fail(function(result) { + result = result || {}; + result.valid = false; + dfd.resolve($field, validatorName, result); + }); + + return dfd; + } + }; +}(jQuery)); +;(function($) { + FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, { + 'en_US': { regexp: { 'default': 'Please enter a value matching the pattern' } } }); @@ -6924,12 +7050,12 @@ * @param {jQuery} $field Field element * @param {Object} options Consists of the following key: * - regexp: The regular expression you need to check * @returns {Boolean} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'regexp'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } var regexp = ('string' === typeof options.regexp) ? new RegExp(options.regexp) : options.regexp; @@ -6960,16 +7086,16 @@ }, /** * Destroy the timer when destroying the FormValidation (using validator.destroy() method) */ - destroy: function(validator, $field, options) { + destroy: function(validator, $field, options, validatorName) { var ns = validator.getNamespace(), - timer = $field.data(ns + '.remote.timer'); + timer = $field.data(ns + '.' + validatorName + '.timer'); if (timer) { clearTimeout(timer); - $field.removeData(ns + '.remote.timer'); + $field.removeData(ns + '.' + validatorName + '.timer'); } }, /** * Request a remote server to check the input value @@ -6992,36 +7118,36 @@ * - url {String|Function} * - validKey {String} [optional]: The valid key. It's "valid" by default * This is useful when connecting to external remote server or APIs provided by 3rd parties * @returns {Deferred} */ - validate: function(validator, $field, options) { + validate: function(validator, $field, options, validatorName) { var ns = validator.getNamespace(), - value = validator.getFieldValue($field, 'remote'), + value = validator.getFieldValue($field, validatorName), dfd = new $.Deferred(); if (value === '') { - dfd.resolve($field, 'remote', { valid: true }); + dfd.resolve($field, validatorName, { valid: true }); return dfd; } var name = $field.attr('data-' + ns + '-field'), data = options.data || {}, url = options.url, validKey = options.validKey || 'valid'; // Support dynamic data if ('function' === typeof data) { - data = data.call(this, validator); + data = data.call(this, validator, $field, value); } // Parse string data from HTML5 attribute if ('string' === typeof data) { data = JSON.parse(data); } // Support dynamic url if ('function' === typeof url) { - url = url.call(this, validator); + url = url.call(this, validator, $field, value); } data[options.name || name] = value; var ajaxOptions = { @@ -7041,33 +7167,33 @@ xhr .success(function(response) { response.valid = (response[validKey] === true || response[validKey] === 'true') ? true : (response[validKey] === false || response[validKey] === 'false' ? false : null); - dfd.resolve($field, 'remote', response); + dfd.resolve($field, validatorName, response); }) .error(function(response) { - dfd.resolve($field, 'remote', { + dfd.resolve($field, validatorName, { valid: false }); }); dfd.fail(function() { xhr.abort(); }); return dfd; } - + if (options.delay) { // Since the form might have multiple fields with the same name // I have to attach the timer to the field element - if ($field.data(ns + '.remote.timer')) { - clearTimeout($field.data(ns + '.remote.timer')); + if ($field.data(ns + '.' + validatorName + '.timer')) { + clearTimeout($field.data(ns + '.' + validatorName + '.timer')); } - $field.data(ns + '.remote.timer', setTimeout(runCallback, options.delay)); + $field.data(ns + '.' + validatorName + '.timer', setTimeout(runCallback, options.delay)); return dfd; } else { return runCallback(); } } @@ -7083,22 +7209,20 @@ }); FormValidation.Validator.rtn = { /** * Validate a RTN (Routing transit number) - * Examples: - * - Valid: 021200025, 789456124 * * @see http://en.wikipedia.org/wiki/Routing_transit_number * @param {FormValidation.Base} validator The validator plugin instance * @param {jQuery} $field Field element * @param {Object} options Can consist of the following keys: * - message: The invalid message * @returns {Boolean} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'rtn'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } if (!/^\d{9}$/.test(value)) { @@ -7125,22 +7249,20 @@ }); FormValidation.Validator.sedol = { /** * Validate a SEDOL (Stock Exchange Daily Official List) - * Examples: - * - Valid: 0263494, B0WNLY7 * * @see http://en.wikipedia.org/wiki/SEDOL * @param {FormValidation.Base} validator The validator plugin instance * @param {jQuery} $field Field element * @param {Object} options Can consist of the following keys: * - message: The invalid message * @returns {Boolean} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'sedol'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } value = value.toUpperCase(); @@ -7176,12 +7298,12 @@ * @param {jQuery} $field Field element * @param {Object} options Consist of key: * - message: The invalid message * @returns {Boolean} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'siren'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } if (!/^\d{9}$/.test(value)) { @@ -7208,12 +7330,12 @@ * @param {jQuery} $field Field element * @param {Object} options Consist of key: * - message: The invalid message * @returns {Boolean} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'siret'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } var sum = 0, @@ -7258,12 +7380,12 @@ * - baseValue: The base value * - step: The step * - message: The invalid message * @returns {Boolean|Object} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'step'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } options = $.extend({}, { baseValue: 0, step: 1 }, options); @@ -7326,12 +7448,12 @@ * @param {Object} options Consist of key: * - message: The invalid message * - case: Can be 'lower' (default) or 'upper' * @returns {Object} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'stringCase'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } var locale = validator.getLocale(), @@ -7396,12 +7518,12 @@ * - message: The invalid message * - trim: Indicate the length will be calculated after trimming the value or not. It is false, by default * - utf8bytes: Evaluate string length in UTF-8 bytes, default to false * @returns {Object} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'stringLength'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (options.trim === true || options.trim === 'true') { value = $.trim(value); } if (value === '') { @@ -7439,15 +7561,15 @@ case (!!min && !!max): message = FormValidation.Helper.format(options.message || FormValidation.I18n[locale].stringLength.between, [parseInt(min, 10), parseInt(max, 10)]); break; case (!!min): - message = FormValidation.Helper.format(options.message || FormValidation.I18n[locale].stringLength.more, parseInt(min, 10)); + message = FormValidation.Helper.format(options.message || FormValidation.I18n[locale].stringLength.more, parseInt(min, 10) - 1); break; case (!!max): - message = FormValidation.Helper.format(options.message || FormValidation.I18n[locale].stringLength.less, parseInt(max, 10)); + message = FormValidation.Helper.format(options.message || FormValidation.I18n[locale].stringLength.less, parseInt(max, 10) + 1); break; default: break; } @@ -7490,12 +7612,12 @@ * - allowLocal: Allow the private and local network IP. Default to false * - allowEmptyProtocol: Allow the URI without protocol. Default to false * - protocol: The protocols, separated by a comma. Default to "http, https, ftp" * @returns {Boolean} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'uri'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } // Credit to https://gist.github.com/dperini/729294 @@ -7604,12 +7726,12 @@ * @param {Object} options Consist of key: * - message: The invalid message * - version: Can be 3, 4, 5, null * @returns {Boolean|Object} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'uuid'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } // See the format at http://en.wikipedia.org/wiki/Universally_unique_identifier#Variants_and_versions @@ -7702,14 +7824,14 @@ * - country: The ISO 3166-1 country code. It can be * - One of country code defined in COUNTRY_CODES * - Name of field which its value defines the country code * - Name of callback function that returns the country code * - A callback function that returns the country code - * @returns {Boolean|Object} + * @returns {Object} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'vat'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } var locale = validator.getLocale(), @@ -7723,26 +7845,22 @@ if ($.inArray(country, this.COUNTRY_CODES) === -1) { return true; } - var method = ['_', country.toLowerCase()].join(''); - return this[method](value) - ? true - : { - valid: false, - message: FormValidation.Helper.format(options.message || FormValidation.I18n[locale].vat.country, FormValidation.I18n[locale].vat.countries[country.toUpperCase()]) - }; + var method = ['_', country.toLowerCase()].join(''), + result = this[method](value); + result = (result === true || result === false) ? { valid: result } : result; + result.message = FormValidation.Helper.format(options.message || FormValidation.I18n[locale].vat.country, FormValidation.I18n[locale].vat.countries[country.toUpperCase()]); + + return result; }, // VAT validators /** * Validate Austrian VAT number - * Example: - * - Valid: ATU13585627 - * - Invalid: ATU13585626 * * @param {String} value VAT number * @returns {Boolean} */ _at: function(value) { @@ -7773,13 +7891,10 @@ return (sum + '' === value.substr(7, 1)); }, /** * Validate Belgian VAT number - * Example: - * - Valid: BE0428759497 - * - Invalid: BE431150351 * * @param {String} value VAT number * @returns {Boolean} */ _be: function(value) { @@ -7801,16 +7916,10 @@ return (sum % 97 === 0); }, /** * Validate Bulgarian VAT number - * Example: - * - Valid: BG175074752, - * BG7523169263, BG8032056031, - * BG7542011030, - * BG7111042925 - * - Invalid: BG175074753, BG7552A10004, BG7111042922 * * @param {String} value VAT number * @returns {Boolean} */ _bg: function(value) { @@ -7895,11 +8004,11 @@ return (egn(value) || pnf(value) || vat(value)); } return false; }, - + /** * Validate Brazilian VAT number (CNPJ) * * @param {String} value VAT number * @returns {Boolean} @@ -7988,13 +8097,10 @@ return (sum + '' === value.substr(8, 1)); }, /** * Validate Cypriot VAT number - * Examples: - * - Valid: CY10259033P - * - Invalid: CY10259033Z * * @param {String} value VAT number * @returns {Boolean} */ _cy: function(value) { @@ -8028,19 +8134,11 @@ return (sum + '' === value.substr(8, 1)); }, /** * Validate Czech Republic VAT number - * Can be: - * i) Legal entities (8 digit numbers) - * ii) Individuals with a RC (the 9 or 10 digit Czech birth number) - * iii) Individuals without a RC (9 digit numbers beginning with 6) * - * Examples: - * - Valid: i) CZ25123891; ii) CZ7103192745, CZ991231123; iii) CZ640903926 - * - Invalid: i) CZ25123890; ii) CZ1103492745, CZ590312123 - * * @param {String} value VAT number * @returns {Boolean} */ _cz: function(value) { if (/^CZ[0-9]{8,10}$/.test(value)) { @@ -8121,13 +8219,10 @@ return false; }, /** * Validate German VAT number - * Examples: - * - Valid: DE136695976 - * - Invalid: DE136695978 * * @param {String} value VAT number * @returns {Boolean} */ _de: function(value) { @@ -8141,13 +8236,10 @@ return FormValidation.Helper.mod11And10(value); }, /** * Validate Danish VAT number - * Example: - * - Valid: DK13585628 - * - Invalid: DK13585627 * * @param {String} value VAT number * @returns {Boolean} */ _dk: function(value) { @@ -8167,13 +8259,10 @@ return (sum % 11 === 0); }, /** * Validate Estonian VAT number - * Examples: - * - Valid: EE100931558, EE100594102 - * - Invalid: EE100594103 * * @param {String} value VAT number * @returns {Boolean} */ _ee: function(value) { @@ -8198,16 +8287,12 @@ * Can be: * i) DNI (Documento nacional de identidad), for Spaniards * ii) NIE (Número de Identificación de Extranjeros), for foreigners * iii) CIF (Certificado de Identificación Fiscal), for legal entities and others * - * Examples: - * - Valid: i) ES54362315K; ii) ESX2482300W, ESX5253868R; iii) ESM1234567L, ESJ99216582, ESB58378431, ESB64717838 - * - Invalid: i) ES54362315Z; ii) ESX2482300A; iii) ESJ99216583 - * * @param {String} value VAT number - * @returns {Boolean} + * @returns {Boolean|Object} */ _es: function(value) { if (/^ES[0-9A-Z][0-9]{7}[0-9A-Z]$/.test(value)) { value = value.substr(2); } @@ -8257,23 +8342,29 @@ return false; }; var first = value.charAt(0); if (/^[0-9]$/.test(first)) { - return dni(value); + return { + valid: dni(value), + type: 'DNI' + }; } else if (/^[XYZ]$/.test(first)) { - return nie(value); + return { + valid: nie(value), + type: 'NIE' + }; } else { - return cif(value); + return { + valid: cif(value), + type: 'CIF' + }; } }, /** * Validate Finnish VAT number - * Examples: - * - Valid: FI20774740 - * - Invalid: FI20774741 * * @param {String} value VAT number * @returns {Boolean} */ _fi: function(value) { @@ -8295,14 +8386,10 @@ /** * Validate French VAT number (TVA - taxe sur la valeur ajoutée) * It's constructed by a SIREN number, prefixed by two characters. * - * Examples: - * - Valid: FR40303265045, FR23334175221, FRK7399859412, FR4Z123456782 - * - Invalid: FR84323140391 - * * @param {String} value VAT number * @returns {Boolean} */ _fr: function(value) { if (/^FR[0-9A-Z]{2}[0-9]{9}$/.test(value)) { @@ -8333,13 +8420,10 @@ } }, /** * Validate United Kingdom VAT number - * Example: - * - Valid: GB980780684 - * - Invalid: GB802311781 * * @param {String} value VAT number * @returns {Boolean} */ _gb: function(value) { @@ -8390,13 +8474,10 @@ return true; }, /** * Validate Greek VAT number - * Examples: - * - Valid: GR023456780, EL094259216 - * - Invalid: EL123456781 * * @param {String} value VAT number * @returns {Boolean} */ _gr: function(value) { @@ -8426,13 +8507,10 @@ return this._gr(value); }, /** * Validate Hungarian VAT number - * Examples: - * - Valid: HU12892312 - * - Invalid: HU12892313 * * @param {String} value VAT number * @returns {Boolean} */ _hu: function(value) { @@ -8453,13 +8531,10 @@ return (sum % 10 === 0); }, /** * Validate Croatian VAT number - * Examples: - * - Valid: HR33392005961 - * - Invalid: HR33392005962 * * @param {String} value VAT number * @returns {Boolean} */ _hr: function(value) { @@ -8473,13 +8548,10 @@ return FormValidation.Helper.mod11And10(value); }, /** * Validate Irish VAT number - * Examples: - * - Valid: IE6433435F, IE6433435OA, IE8D79739I - * - Invalid: IE8D79738J * * @param {String} value VAT number * @returns {Boolean} */ _ie: function(value) { @@ -8515,13 +8587,10 @@ return true; }, /** * Validate Icelandic VAT (VSK) number - * Examples: - * - Valid: 12345, 123456 - * - Invalid: 1234567 * * @params {String} value VAT number * @returns {Boolean} */ _is: function(value) { @@ -8535,14 +8604,10 @@ * Validate Italian VAT number, which consists of 11 digits. * - First 7 digits are a company identifier * - Next 3 are the province of residence * - The last one is a check digit * - * Examples: - * - Valid: IT00743110157 - * - Invalid: IT00743110158 - * * @param {String} value VAT number * @returns {Boolean} */ _it: function(value) { if (/^IT[0-9]{11}$/.test(value)) { @@ -8568,14 +8633,10 @@ * Validate Lithuanian VAT number * It can be: * - 9 digits, for legal entities * - 12 digits, for temporarily registered taxpayers * - * Examples: - * - Valid: LT119511515, LT100001919017, LT100004801610 - * - Invalid: LT100001919018 - * * @param {String} value VAT number * @returns {Boolean} */ _lt: function(value) { if (/^LT([0-9]{7}1[0-9]{1}|[0-9]{10}1[0-9]{1})$/.test(value)) { @@ -8602,13 +8663,10 @@ return (check + '' === value.charAt(length - 1)); }, /** * Validate Luxembourg VAT number - * Examples: - * - Valid: LU15027442 - * - Invalid: LU15027443 * * @param {String} value VAT number * @returns {Boolean} */ _lu: function(value) { @@ -8622,13 +8680,10 @@ return ((parseInt(value.substr(0, 6), 10) % 89) + '' === value.substr(6, 2)); }, /** * Validate Latvian VAT number - * Examples: - * - Valid: LV40003521600, LV16117519997 - * - Invalid: LV40003521601, LV16137519997 * * @param {String} value VAT number * @returns {Boolean} */ _lv: function(value) { @@ -8675,13 +8730,10 @@ } }, /** * Validate Maltese VAT number - * Examples: - * - Valid: MT11679112 - * - Invalid: MT11679113 * * @param {String} value VAT number * @returns {Boolean} */ _mt: function(value) { @@ -8702,13 +8754,10 @@ return (sum % 37 === 0); }, /** * Validate Dutch VAT number - * Examples: - * - Valid: NL004495445B01 - * - Invalid: NL123456789B90 * * @param {String} value VAT number * @returns {Boolean} */ _nl: function(value) { @@ -8760,13 +8809,10 @@ return (sum + '' === value.substr(8, 1)); }, /** * Validate Polish VAT number - * Examples: - * - Valid: PL8567346215 - * - Invalid: PL8567346216 * * @param {String} value VAT number * @returns {Boolean} */ _pl: function(value) { @@ -8787,13 +8833,10 @@ return (sum % 11 === 0); }, /** * Validate Portuguese VAT number - * Examples: - * - Valid: PT501964843 - * - Invalid: PT501964842 * * @param {String} value VAT number * @returns {Boolean} */ _pt: function(value) { @@ -8817,13 +8860,10 @@ return (sum + '' === value.substr(8, 1)); }, /** * Validate Romanian VAT number - * Examples: - * - Valid: RO18547290 - * - Invalid: RO18547291 * * @param {String} value VAT number * @returns {Boolean} */ _ro: function(value) { @@ -8924,13 +8964,10 @@ return ((sum + parseInt(value.substr(8, 1), 10)) % 10 === 1); }, /** * Validate Swedish VAT number - * Examples: - * - Valid: SE123456789701 - * - Invalid: SE123456789101 * * @param {String} value VAT number * @returns {Boolean} */ _se: function(value) { @@ -8945,14 +8982,10 @@ return FormValidation.Helper.luhn(value); }, /** * Validate Slovenian VAT number - * Examples: - * - Valid: SI50223054 - * - Invalid: SI50223055 - * - Invalid: SI09999990 * * @param {String} value VAT number * @returns {Boolean} */ _si: function(value) { @@ -8978,13 +9011,10 @@ return (sum + '' === value.substr(7, 1)); }, /** * Validate Slovak VAT number - * Examples: - * - Valid: SK2022749619 - * - Invalid: SK2022749618 * * @param {String} value VAT number * @returns {Boolean} */ _sk: function(value) { @@ -8998,13 +9028,10 @@ return (parseInt(value, 10) % 11 === 0); }, /** * Validate Venezuelan VAT number (RIF) - * Examples: - * - Valid: VEJ309272292, VEV242818101, VEJ000126518, VEJ000458324, J309272292, V242818101, J000126518, J000458324 - * - Invalid: VEJ309272293, VEV242818100, J000126519, J000458323 * * @param {String} value VAT number * @returns {Boolean} */ _ve: function(value) { @@ -9036,13 +9063,10 @@ return (sum + '' === value.substr(9, 1)); }, /** * Validate South African VAT number - * Examples: - * - Valid: 4012345678 - * - Invalid: 40123456789, 3012345678 * * @params {String} value VAT number * @returns {Boolean} */ _za: function(value) { @@ -9071,12 +9095,12 @@ * @param {jQuery} $field Field element * @param {Object} options Consist of key: * - message: The invalid message * @returns {Boolean} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'vin'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '') { return true; } // Don't accept I, O, Q characters @@ -9166,18 +9190,18 @@ * - Name of callback function that returns the country code * - A callback function that returns the country code * * callback: function(value, validator, $field) { * // value is the value of field - * // validator is the BootstrapValidator instance + * // validator is the FormValidation.Base instance * // $field is jQuery element representing the field * } * * @returns {Boolean|Object} */ - validate: function(validator, $field, options) { - var value = validator.getFieldValue($field, 'zipCode'); + validate: function(validator, $field, options, validatorName) { + var value = validator.getFieldValue($field, validatorName); if (value === '' || !options.country) { return true; } var locale = validator.getLocale(), @@ -9316,13 +9340,9 @@ }; }, /** * Validate United Kingdom postcode - * Examples: - * - Standard: EC1A 1BB, W1A 1HQ, M1 1AA, B33 8TH, CR2 6XH, DN55 1PT - * - Special cases: - * AI-2640, ASCN 1ZZ, GIR 0AA * * @see http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom * @param {String} value The postcode * @returns {Boolean} */