|
5 | 5 | * |
6 | 6 | * @website http://formvalidator.net/ |
7 | 7 | * @license Dual licensed under the MIT or GPL Version 2 licenses |
8 | | -* @version 2.2.0 |
| 8 | +* @version 2.2.beta.11 |
9 | 9 | */ |
10 | 10 | (function($) { |
11 | 11 |
|
|
97 | 97 | $.fn.validateOnBlur = function(language, settings) { |
98 | 98 | this.find('input[data-validation],textarea[data-validation],select[data-validation]') |
99 | 99 | .bind('blur.validation', function() { |
100 | | - $(this).validateInputOnBlur(language, settings); |
| 100 | + $(this).validateInputOnBlur(language, settings, true, 'blur'); |
101 | 101 | }); |
102 | 102 |
|
103 | 103 | return this; |
|
116 | 116 | etype = $el.attr("data-validation-event"); |
117 | 117 | if (etype){ |
118 | 118 | $el.bind(etype + ".validation", function(){ |
119 | | - $(this).validateInputOnBlur(language, settings, false, etype); |
| 119 | + $(this).validateInputOnBlur(language, settings, true, etype); |
120 | 120 | }); |
121 | 121 | } |
122 | 122 | }); |
|
139 | 139 | // Remove previously added event listeners |
140 | 140 | this.find('.has-help-txt') |
141 | 141 | .valAttr('has-keyup-event', false) |
142 | | - .valAttr('backend-valid', false) |
143 | | - .valAttr('backend-invalid', false) |
144 | 142 | .removeClass('has-help-txt'); |
145 | 143 |
|
146 | 144 | // Add help text listeners |
|
188 | 186 | * |
189 | 187 | * @param {Object} [language] Optional, will override $.formUtils.LANG |
190 | 188 | * @param {Object} [conf] Optional, will override the default settings |
191 | | - * @param {Boolean} [attachKeyupEvent] Optional |
192 | | - * @param {String} [eventContext] |
| 189 | + * @param {Boolean} attachKeyupEvent Optional |
| 190 | + * @param {String} eventType |
193 | 191 | * @return {jQuery} |
194 | 192 | */ |
195 | | - $.fn.validateInputOnBlur = function(language, conf, attachKeyupEvent, eventContext) { |
196 | | - if(attachKeyupEvent === undefined) |
197 | | - attachKeyupEvent = true; |
198 | | - if(!eventContext) |
199 | | - eventContext = 'blur'; |
| 193 | + $.fn.validateInputOnBlur = function(language, conf, attachKeyupEvent, eventType) { |
| 194 | + console.log(eventType); |
| 195 | + $.formUtils.eventType = eventType; |
200 | 196 |
|
201 | 197 | if( (this.valAttr('suggestion-nr') || this.valAttr('postpone') || this.hasClass('hasDatepicker')) && !window.postponedValidation ) { |
202 | | - // This validation has to be postponed |
| 198 | + // This validation has to be postponed |
203 | 199 | var _self = this, |
204 | 200 | postponeTime = this.valAttr('postpone') || 200; |
205 | 201 |
|
206 | 202 | window.postponedValidation = function() { |
207 | | - _self.validateInputOnBlur(language, conf, attachKeyupEvent); |
| 203 | + _self.validateInputOnBlur(language, conf, attachKeyupEvent, eventType); |
208 | 204 | window.postponedValidation = false; |
209 | 205 | }; |
210 | 206 | setTimeout(function() { |
|
218 | 214 |
|
219 | 215 | language = $.extend({}, $.formUtils.LANG, language || {}); |
220 | 216 | _removeErrorStyle(this, conf); |
221 | | - |
222 | 217 | var $elem = this, |
223 | 218 | $form = $elem.closest("form"), |
224 | 219 | validationRule = $elem.attr(conf.validationRuleAttribute), |
|
227 | 222 | language, |
228 | 223 | $.extend({}, conf, {errorMessagePosition:'element'}), |
229 | 224 | $form, |
230 | | - eventContext |
| 225 | + eventType |
231 | 226 | ); |
232 | | - |
233 | | - $elem.trigger('validation', [validation===null ? null : validation===true]); |
234 | | - |
| 227 | + |
235 | 228 | if(validation === true) { |
236 | 229 | $elem |
237 | 230 | .addClass('valid') |
|
243 | 236 | _setInlineErrorMessage($elem, validation, conf, conf.errorMessagePosition); |
244 | 237 |
|
245 | 238 | if(attachKeyupEvent) { |
246 | | - $elem.bind('keyup', function() { |
247 | | - $(this).validateInputOnBlur(language, conf, false, 'keyup'); |
248 | | - }); |
| 239 | + $elem |
| 240 | + .unbind('keyup.validation') |
| 241 | + .bind('keyup.validation', function() { |
| 242 | + $(this).validateInputOnBlur(language, conf, false, 'keyup'); |
| 243 | + }); |
249 | 244 | } |
250 | 245 | } |
251 | 246 |
|
|
358 | 353 | 'submit' |
359 | 354 | ); |
360 | 355 |
|
361 | | - $elem.trigger('validation', [validation===true]); |
362 | | - |
363 | 356 | // Run element validation callback |
364 | 357 | if( typeof conf.onElementValidate == 'function' ) { |
365 | 358 | conf.onElementValidate((validation === true), $elem, $form, validation); |
|
790 | 783 |
|
791 | 784 | /** |
792 | 785 | * Validate the value of given element according to the validation rules |
793 | | - * found in the attribute data-validation. Will return true if valid, |
794 | | - * error message otherwise |
| 786 | + * found in the attribute data-validation. Will return null if no validation |
| 787 | + * should take place, returns true if valid or error message if not valid |
795 | 788 | * |
796 | 789 | * @param {jQuery} $elem |
797 | 790 | * @param {Object} language ($.formUtils.LANG) |
|
865 | 858 | $elem = $("[name='"+$elem.attr('name')+"']:eq(0)"); |
866 | 859 | } |
867 | 860 |
|
868 | | - var isValid = true; |
| 861 | + var isValid = null; |
869 | 862 | if( eventContext != 'keyup' || validator.validateOnKeyUp ) { |
870 | 863 | isValid = validator.validatorFunction(value, $elem, conf, language, $form); |
871 | 864 | } |
872 | 865 |
|
873 | 866 | if(!isValid) { |
874 | | - validationErrorMsg = $elem.attr(conf.validationErrorMsgAttribute+'-'+rule.replace('validate_', '')); |
875 | | - if( !validationErrorMsg ) { |
876 | | - validationErrorMsg = $elem.attr(conf.validationErrorMsgAttribute); |
| 867 | + validationErrorMsg = null; |
| 868 | + if( isValid !== null ) { |
| 869 | + validationErrorMsg = $elem.attr(conf.validationErrorMsgAttribute+'-'+rule.replace('validate_', '')); |
877 | 870 | if( !validationErrorMsg ) { |
878 | | - validationErrorMsg = language[validator.errorMessageKey]; |
879 | | - if( !validationErrorMsg ) |
880 | | - validationErrorMsg = validator.errorMessage; |
| 871 | + validationErrorMsg = $elem.attr(conf.validationErrorMsgAttribute); |
| 872 | + if( !validationErrorMsg ) { |
| 873 | + validationErrorMsg = language[validator.errorMessageKey]; |
| 874 | + if( !validationErrorMsg ) |
| 875 | + validationErrorMsg = validator.errorMessage; |
| 876 | + } |
881 | 877 | } |
882 | 878 | } |
883 | 879 | return false; // breaks the iteration |
|
890 | 886 | }, ' '); |
891 | 887 |
|
892 | 888 | if( typeof validationErrorMsg == 'string' ) { |
| 889 | + $elem.trigger('validation', false); |
893 | 890 | return validationErrorMsg; |
| 891 | + } else if( validationErrorMsg === null && !conf.addValidClassOnAll ) { |
| 892 | + return null; |
894 | 893 | } else { |
| 894 | + $elem.trigger('validation', true); |
895 | 895 | return true; |
896 | 896 | } |
897 | 897 | }, |
|
0 commit comments