UNPKG

formvalidation

Version:

The best jQuery plugin to validate form fields. Support Bootstrap, Foundation, Pure, SemanticUI, UIKit and custom frameworks

98 lines (89 loc) 3.73 kB
/** * different validator * * @link http://formvalidation.io/validators/different/ * @author https://twitter.com/nghuuphuoc * @copyright (c) 2013 - 2015 Nguyen Huu Phuoc * @license http://formvalidation.io/license/ */ (function($) { FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, { 'en_US': { different: { 'default': 'Please enter a different value' } } }); FormValidation.Validator.different = { html5Attributes: { message: 'message', field: 'field' }, /** * Bind the validator on the live change of the field to compare with current one * * @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) { 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'); if (status !== validator.STATUS_NOT_VALIDATED) { validator.revalidateField($field); } }); } }, /** * Unbind the validator on the live change of the field to compare with current one * * @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) { var fields = options.field.split(','); for (var i = 0; i < fields.length; i++) { var compareWith = validator.getFieldElements(fields[i]); validator.offLiveChange(compareWith, 'live_different'); } }, /** * Return true if the input value is different with given field's value * * @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 * - message: The invalid message * @returns {Boolean} */ validate: function(validator, $field, options) { var value = validator.getFieldValue($field, 'different'); if (value === '') { return true; } var fields = options.field.split(','), isValid = true; for (var i = 0; i < fields.length; i++) { var compareWith = validator.getFieldElements(fields[i]); if (compareWith == null || compareWith.length === 0) { continue; } var compareValue = validator.getFieldValue(compareWith, 'different'); if (value === compareValue) { isValid = false; } else if (compareValue !== '') { validator.updateStatus(compareWith, validator.STATUS_VALID, 'different'); } } return isValid; } }; }(jQuery));