1'use strict'; 2 3/** 4 * jQuery Form Validator Module: Logic 5 * ------------------------------------------ 6 * Created by Victor Jonsson <http://www.victorjonsson.se> 7 * 8 * - data-validation-depends-on 9 * - data-validation-if-answered 10 * 11 * @website http://formvalidator.net/#logic 12 * @license MIT 13 */ 14(function ($) { 15 16 'use strict'; 17 18 var setupValidationDependsOn = function setupValidationDependsOn($form, conf) { 19 20 var dependingOnBeforeValidation = function dependingOnBeforeValidation() { 21 22 var $input = $(this), 23 nameOfDependingInput = $input.valAttr('depends-on') || $input.valAttr('if-checked'); 24 25 // Whether or not this input should be validated depends on if another input has a value 26 if (nameOfDependingInput) { 27 28 // Set the boolean telling us that the validation depends 29 // on another input being checked 30 var valueOfDependingInput = $.formUtils.getValue('[name="' + nameOfDependingInput + '"]', $form), 31 listWithRequiredValues = $.split($input.valAttr('depends-on-value'), false, false), 32 dependingInputIsMissingValueOrHasIncorrectValue = !valueOfDependingInput || listWithRequiredValues.length && !valueIsInList(valueOfDependingInput, listWithRequiredValues); 33 34 if (dependingInputIsMissingValueOrHasIncorrectValue) { 35 $input.valAttr('skipped', '1'); 36 } 37 } 38 }, 39 valueIsInList = function valueIsInList(value, valueList) { 40 var isInList = false, 41 lowerCaseValue = value.toLocaleLowerCase(); 42 43 $.each(valueList, function (i, otherValue) { 44 if (lowerCaseValue === otherValue.toLocaleLowerCase()) { 45 isInList = true; 46 return false; 47 } 48 }); 49 return isInList; 50 },
51 dependingOnValueChanged = function dependingOnValueChanged() { 52 var $input = $(this), 53 $otherInput = this.$dependingInput, 54 valueOfDependingInput = $.formUtils.getValue($input), 55 requiredValueOfDependingInput = $input.valAttr('depending-value'), 56 otherInputHasValue = $.formUtils.getValue($otherInput) ? true : false, 57 dependingInputIsMissingValueOrHasIncorrectValue = !valueOfDependingInput || requiredValueOfDependingInput && requiredValueOfDependingInput !== valueOfDependingInput; 58 59 if (dependingInputIsMissingValueOrHasIncorrectValue && !otherInputHasValue) { 60 $.formUtils.dialogs.removeInputStylingAndMessage($otherInput, conf); 61 } 62 }; 63 64 $form.find('[data-validation-depends-on]').off('beforeValidation', dependingOnBeforeValidation).on('beforeValidation', dependingOnBeforeValidation).each(function () { 65 // Remove validation when on depending input 66 var $dependingInput = $(this); 67 $form.find('[name="' + $dependingInput.valAttr('depends-on') + '"]').each(function () { 68 $(this).off('change', dependingOnValueChanged).on('change', dependingOnValueChanged).valAttr('depending-value', $dependingInput.valAttr('depends-on-value')); 69 70 this.$dependingInput = $dependingInput; 71 }); 72 }); 73 }, 74 setupValidationTogetherWith = function setupValidationTogetherWith($form, conf) { 75 76 var optionalBeforeValidation = function optionalBeforeValidation() { 77 var $input = $(this), 78 dependingInputs = $input.valAttr('optional-if-answered'), 79 dependingInputsHasValue = false, 80 thisInputHasAnswer = $.formUtils.getValue($input) ? true : false; 81 82 if (!thisInputHasAnswer) { 83 $.each($.split(dependingInputs), function (i, inputName) { 84 var $dependingInput = $form.find('[name="' + inputName + '"]'); 85 dependingInputsHasValue = $.formUtils.getValue($dependingInput) ? true : false; 86 if (dependingInputsHasValue) { 87 return false; 88 } 89 }); 90 91 if (dependingInputsHasValue) { 92 $input.valAttr('skipped', 1); 93 } 94 } 95 }, 96 optionalInputOnChange = function optionalInputOnChange() { 97 var $input = $(this), 98 dependingInputs = $input.valAttr('optional-if-answered'); 99 100 $.each($.split(dependingInputs), function (i, inputName) { 101 var $dependingInput = $form.find('[name="' + inputName + '"]'), 102 dependingInputsHasValue = $.formUtils.getValue($dependingInput) ? true : false; 103 if (!dependingInputsHasValue) { 104 $.formUtils.dialogs.removeInputStylingAndMessage($dependingInput, conf); 105 } 106 }); 107 }; 108 109 $form.find('[data-validation-optional-if-answered]').off('beforeValidation', optionalBeforeValidation).on('beforeValidation', optionalBeforeValidation).each(function () { 110 $(this).off('change', optionalInputOnChange).on('change', optionalInputOnChange); 111 }); 112 }; 113 114 $.formUtils.$win.bind('validatorsLoaded formValidationSetup', function (evt, $form, conf) { 115 if (!$form) { 116 $form = $('form'); 117 } 118 setupValidationDependsOn($form, conf); 119 setupValidationTogetherWith($form, conf); 120 }); 121})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.