Skip to content

Commit 4e38a72

Browse files
committed
Fix for issue victorjonsson#73
1 parent 1a6a6c0 commit 4e38a72

File tree

9 files changed

+90
-67
lines changed

9 files changed

+90
-67
lines changed

form-validator/date.dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
* @website http://formvalidator.net/#location-validators
1212
* @license Dual licensed under the MIT or GPL Version 2 licenses
13-
* @version 2.1.28
13+
* @version 2.1.29
1414
*/
1515
(function($) {
1616

form-validator/file.dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
* @website http://formvalidator.net/
1212
* @license Dual licensed under the MIT or GPL Version 2 licenses
13-
* @version 2.1.28
13+
* @version 2.1.29
1414
*/
1515
(function($, window) {
1616

form-validator/form-test.html

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
/* Form and inputs */
1010
form {
1111
width: 500px;
12+
margin: 0 auto;
1213
padding: 20px;
14+
display: block;
1315
}
1416

1517
input.form-control {
@@ -155,6 +157,18 @@
155157

156158
var dev = window.location.hash.indexOf('dev') > -1 ? '.dev' : '';
157159

160+
161+
// Add a new validator
162+
$.formUtils.addValidator({
163+
name : 'even_number',
164+
validatorFunction : function(value, $el, config, language, $form) {
165+
return parseInt(value, 10) % 2 === 0;
166+
},
167+
borderColorOnError : '',
168+
errorMessage : 'You have to give an even number',
169+
errorMessageKey: 'badEvenNumber'
170+
});
171+
158172
window.applyValidation = function() {
159173
$.validate({
160174
language : {
@@ -164,12 +178,12 @@
164178
scrollToTopOnError : true,
165179
modules : 'security'+dev+', location'+dev+', sweden'+dev+', file'+dev+', uk'+dev,
166180
onModulesLoaded: function( $form ) {
167-
console.log('wii');
168181
$('#country-suggestions').suggestCountry();
169182
$('#swedish-county-suggestions').suggestSwedishCounty();
170183
$('#password').displayPasswordStrength();
171184
},
172185
onValidate : function() {
186+
console.log('wii');
173187
var $callbackInput = $('#callback');
174188
if( $callbackInput.val() == 1 ) {
175189
return {
@@ -195,16 +209,6 @@
195209
// Load one module outside $.validate() even though you do not have to
196210
$.formUtils.loadModules('date'+dev+'.js', false, false);
197211

198-
// Add a new validator
199-
$.formUtils.addValidator({
200-
name : 'even_number',
201-
validatorFunction : function(value, $el, config, language, $form) {
202-
return parseInt(value, 10) % 2 === 0;
203-
},
204-
borderColorOnError : '',
205-
errorMessage : 'You have to give an even number',
206-
errorMessageKey: 'badEvenNumber'
207-
});
208212

209213
})(jQuery, window);
210214
</script>

form-validator/jquery.form-validator.js

Lines changed: 67 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* @website http://formvalidator.net/
77
* @license Dual licensed under the MIT or GPL Version 2 licenses
8-
* @version 2.1.28
8+
* @version 2.1.29
99
*/
1010
(function($) {
1111

@@ -19,14 +19,11 @@
1919
* @return {jQuery}
2020
*/
2121
$.fn.validateOnBlur = function(language, settings) {
22-
23-
var blurValidationCallback = function() {
24-
$(this).validateInputOnBlur(language, settings);
25-
};
26-
2722
this.find('input[data-validation], textarea[data-validation]')
28-
.unbind('blur', blurValidationCallback)
29-
.bind('blur', blurValidationCallback);
23+
.unbind('blur.validation')
24+
.bind('blur.validation', function() {
25+
$(this).validateInputOnBlur(language, settings);
26+
});
3027

3128
return this;
3229
};
@@ -49,20 +46,20 @@
4946
.valAttr('has-keyup-event', false)
5047
.valAttr('backend-valid', false)
5148
.valAttr('backend-invalid', false)
52-
.unbind('focus')
53-
.unbind('blur')
49+
.unbind('focus.validation')
50+
.unbind('blur.validation')
5451
.removeClass('has-help-txt');
5552

5653
// Add help text listeners
5754
this.find('textarea,input').each(function() {
5855
var $element = $(this),
59-
className = 'jquery_form_help_' + $element.attr('name').replace( /(:|\.|\[|\])/g, "" ),
56+
className = 'jquery_form_help_' + ($element.attr('name') || '').replace( /(:|\.|\[|\])/g, "" ),
6057
help = $element.attr(attrName);
6158

6259
if(help) {
6360
$element
6461
.addClass('has-help-txt')
65-
.focus(function() {
62+
.bind('focus.validation', function() {
6663
var $help = $element.parent().find('.'+className);
6764
if($help.length == 0) {
6865
$help = $('<span />')
@@ -76,7 +73,7 @@
7673
}
7774
$help.fadeIn();
7875
})
79-
.blur(function() {
76+
.bind('blur.validation', function() {
8077
$(this)
8178
.parent()
8279
.find('.'+className)
@@ -440,37 +437,38 @@
440437
onError : false
441438
}, config || {});
442439

443-
var formSubmitCallback = function() {
444-
var $form = $(this);
445-
if($.formUtils.isLoadingModules) {
446-
setTimeout(function() {
447-
$form.trigger('submit');
448-
}, 200);
449-
return false;
450-
}
451-
var valid = $(this).validateForm(config.language, config);
452-
if( valid && typeof config.onSuccess == 'function') {
453-
var callbackResponse = config.onSuccess($form);
454-
if( callbackResponse === false )
455-
return false;
456-
} else if ( !valid && typeof config.onError == 'function' ) {
457-
config.onError($form);
458-
return false;
459-
} else {
460-
return valid;
461-
}
462-
};
463-
464440
// Remove all event listeners previously added
465-
$('form').unbind('submit', formSubmitCallback);
441+
$('form.has-validation-callback')
442+
.removeClass('has-validation-callback')
443+
.unbind('submit.validation');
466444

467445
// Add validation to forms
468446
$.split(config.form, function(formQuery) {
469447

470448
var $form = $(formQuery);
471449

472450
// Validate when submitted
473-
$form.bind('submit', formSubmitCallback);
451+
$form.bind('submit.validation', function() {
452+
var $form = $(this);
453+
if($.formUtils.isLoadingModules) {
454+
setTimeout(function() {
455+
$form.trigger('submit.validation');
456+
}, 200);
457+
return false;
458+
}
459+
var valid = $(this).validateForm(config.language, config);
460+
if( valid && typeof config.onSuccess == 'function') {
461+
var callbackResponse = config.onSuccess($form);
462+
if( callbackResponse === false )
463+
return false;
464+
} else if ( !valid && typeof config.onError == 'function' ) {
465+
config.onError($form);
466+
return false;
467+
} else {
468+
return valid;
469+
}
470+
})
471+
.addClass('has-validation-callback');
474472

475473
if( config.showHelpOnFocus ) {
476474
$form.showHelpOnFocus();
@@ -966,6 +964,14 @@
966964
activeSuggestionCSS : {
967965
background : '#E9E9E9'
968966
}
967+
},
968+
setSuggsetionPosition = function($suggestionContainer, $input) {
969+
var offset = $input.offset();
970+
$suggestionContainer.css({
971+
width : $input.outerWidth(),
972+
left : offset.left + 'px',
973+
top : (offset.top + $input.outerHeight()) +'px'
974+
});
969975
};
970976

971977
if(settings)
@@ -975,6 +981,17 @@
975981
config.css['z-index'] = 9999;
976982
$element.attr('autocomplete', 'off');
977983

984+
if( this._numSuggestionElements === 0 ) {
985+
// Re-position suggestion container if window size changes
986+
$(window).bind('resize', function() {
987+
$('.jquery-form-suggestions').each(function() {
988+
var $container = $(this),
989+
suggestID = $container.attr('data-suggest-container');
990+
setSuggsetionPosition($container, $('.suggestions-'+suggestID).eq(0));
991+
});
992+
});
993+
}
994+
978995
this._numSuggestionElements++;
979996

980997
var onSelectSuggestion = function($el) {
@@ -987,11 +1004,13 @@
9871004
$element
9881005
.data('suggestions', suggestions)
9891006
.valAttr('suggestion-nr', this._numSuggestionElements)
990-
.bind('focus', function() {
1007+
.unbind('focus.validation')
1008+
.bind('focus.validation', function() {
9911009
$(this).trigger('keyup');
9921010
$.formUtils._selectedSuggestion = null;
9931011
})
994-
.bind('keyup', function() {
1012+
.unbind('keyup.validation')
1013+
.bind('keyup.validation', function() {
9951014
var $input = $(this),
9961015
foundSuggestions = [],
9971016
val = $.trim($input.val()).toLocaleLowerCase();
@@ -1032,8 +1051,11 @@
10321051
// Create suggestion container if not already exists
10331052
else if(foundSuggestions.length > 0 && $suggestionContainer.length == 0) {
10341053
$suggestionContainer = $('<div></div>').css(config.css).appendTo('body');
1035-
$suggestionContainer.addClass('jquery-form-suggestions');
1036-
$suggestionContainer.addClass('jquery-form-suggestion-'+suggestionId);
1054+
$element.addClass('suggestions-'+suggestionId);
1055+
$suggestionContainer
1056+
.attr('data-suggest-container', suggestionId)
1057+
.addClass('jquery-form-suggestions')
1058+
.addClass('jquery-form-suggestion-'+suggestionId);
10371059
}
10381060

10391061
// Show hidden container
@@ -1045,12 +1067,7 @@
10451067
if(foundSuggestions.length > 0 && val.length != foundSuggestions[0].length) {
10461068

10471069
// put container in place every time, just in case
1048-
var offset = $input.offset();
1049-
$suggestionContainer.css({
1050-
width : $input.outerWidth(),
1051-
left : offset.left + 'px',
1052-
top : (offset.top + $input.outerHeight()) +'px'
1053-
});
1070+
setSuggsetionPosition($suggestionContainer, $input);
10541071

10551072
// Add suggestions HTML to container
10561073
$suggestionContainer.html('');
@@ -1073,7 +1090,8 @@
10731090
});
10741091
}
10751092
})
1076-
.bind('keydown', function(e) {
1093+
.unbind('keydown.validation')
1094+
.bind('keydown.validation', function(e) {
10771095
var code = (e.keyCode ? e.keyCode : e.which),
10781096
suggestionId,
10791097
$suggestionContainer,
@@ -1134,7 +1152,8 @@
11341152
}
11351153
}
11361154
})
1137-
.bind('blur', function() {
1155+
.unbind('blur.validation')
1156+
.bind('blur.validation', function() {
11381157
onSelectSuggestion($(this));
11391158
});
11401159

form-validator/jquery.form-validator.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

form-validator/location.dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
* @website http://formvalidator.net/#location-validators
1212
* @license Dual licensed under the MIT or GPL Version 2 licenses
13-
* @version 2.1.28
13+
* @version 2.1.29
1414
*/
1515
(function($) {
1616

form-validator/security.dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*
1313
* @website http://formvalidator.net/#security-validators
1414
* @license Dual licensed under the MIT or GPL Version 2 licenses
15-
* @version 2.1.28
15+
* @version 2.1.29
1616
*/
1717
(function($) {
1818

form-validator/sweden.dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
* @website http://formvalidator.net/#swedish-validators
1515
* @license Dual licensed under the MIT or GPL Version 2 licenses
16-
* @version 2.1.28
16+
* @version 2.1.29
1717
*/
1818
(function($, window) {
1919

form-validator/uk.dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
* @website http://formvalidator.net/#uk-validators
1111
* @license Dual licensed under the MIT or GPL Version 2 licenses
12-
* @version 2.1.28
12+
* @version 2.1.29
1313
*/
1414
$.formUtils.addValidator({
1515
name : 'ukvatnumber',

0 commit comments

Comments
 (0)