|
5 | 5 | * |
6 | 6 | * @website http://formvalidator.net/ |
7 | 7 | * @license Dual licensed under the MIT or GPL Version 2 licenses |
8 | | -* @version 2.1.25 |
| 8 | +* @version 2.1.26 |
9 | 9 | */ |
10 | 10 | (function($) { |
11 | 11 |
|
|
19 | 19 | * @return {jQuery} |
20 | 20 | */ |
21 | 21 | $.fn.validateOnBlur = function(language, settings) { |
| 22 | + |
| 23 | + var blurValidationCallback = function() { |
| 24 | + $(this).validateInputOnBlur(language, settings); |
| 25 | + }; |
| 26 | + |
22 | 27 | this.find('input[data-validation], textarea[data-validation]') |
23 | | - .blur(function() { |
24 | | - $(this).validateInputOnBlur(language, settings); |
25 | | - }); |
| 28 | + .unbind('blur', blurValidationCallback) |
| 29 | + .bind('blur', blurValidationCallback); |
| 30 | + |
26 | 31 | return this; |
27 | 32 | }; |
28 | 33 |
|
|
39 | 44 | attrName = 'data-validation-help'; |
40 | 45 | } |
41 | 46 |
|
| 47 | + // Remove previously added event listeners |
| 48 | + this.find('.has-help-txt') |
| 49 | + .valAttr('has-keyup-event', false) |
| 50 | + .valAttr('backend-valid', false) |
| 51 | + .valAttr('backend-invalid', false) |
| 52 | + .unbind('focus') |
| 53 | + .unbind('blur') |
| 54 | + .removeClass('has-help-txt'); |
| 55 | + |
| 56 | + // Add help text listeners |
42 | 57 | this.find('textarea,input').each(function() { |
43 | 58 | var $element = $(this), |
44 | 59 | className = 'jquery_form_help_' + $element.attr('name'), |
45 | 60 | help = $element.attr(attrName); |
| 61 | + |
46 | 62 | if(help) { |
47 | 63 | $element |
| 64 | + .addClass('has-help-txt') |
48 | 65 | .focus(function() { |
49 | 66 | var $help = $element.parent().find('.'+className); |
50 | 67 | if($help.length == 0) { |
|
367 | 384 |
|
368 | 385 | sugs = $.split($field.attr('data-suggestions')); |
369 | 386 |
|
370 | | - if( sugs.length > 0 ) { |
| 387 | + if( sugs.length > 0 && !$field.hasClass('has-suggestions') ) { |
371 | 388 | $.formUtils.suggest($field, sugs, settings); |
| 389 | + $field.addClass('has-suggestions'); |
372 | 390 | } |
373 | 391 | }); |
374 | 392 | return this; |
|
422 | 440 | onError : false |
423 | 441 | }, config || {}); |
424 | 442 |
|
| 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 | + |
| 464 | + // Remove all event listeners previously added |
| 465 | + $('form').unbind('submit', formSubmitCallback); |
| 466 | + |
| 467 | + // Add validation to forms |
425 | 468 | $.split(config.form, function(formQuery) { |
| 469 | + |
426 | 470 | var $form = $(formQuery); |
427 | 471 |
|
428 | 472 | // Validate when submitted |
429 | | - $form.bind('submit', function() { |
430 | | - if($.formUtils.isLoadingModules) { |
431 | | - setTimeout(function() { |
432 | | - $form.trigger('submit'); |
433 | | - }, 200); |
434 | | - return false; |
435 | | - } |
436 | | - var valid = $(this).validateForm(config.language, config); |
437 | | - if( valid && typeof config.onSuccess == 'function') { |
438 | | - var callbackResponse = config.onSuccess($form); |
439 | | - if( callbackResponse === false ) |
440 | | - return false; |
441 | | - } else if ( !valid && typeof config.onError == 'function' ) { |
442 | | - config.onError($form); |
443 | | - return false; |
444 | | - } else { |
445 | | - return valid; |
446 | | - } |
447 | | - }); |
| 473 | + $form.bind('submit', formSubmitCallback); |
448 | 474 |
|
449 | | - if( config.validateOnBlur ) { |
450 | | - $form.validateOnBlur(config.language, config); |
451 | | - } |
452 | 475 | if( config.showHelpOnFocus ) { |
453 | 476 | $form.showHelpOnFocus(); |
454 | 477 | } |
455 | 478 | if( config.addSuggestions ) { |
456 | 479 | $form.addSuggestions(); |
457 | 480 | } |
| 481 | + if( config.validateOnBlur ) { |
| 482 | + $form.validateOnBlur(config.language, config); |
| 483 | + } |
458 | 484 | }); |
459 | 485 |
|
460 | 486 | if( config.modules != '' ) { |
|
564 | 590 | */ |
565 | 591 | isLoadingModules : false, |
566 | 592 |
|
| 593 | + loadedModules : {}, |
| 594 | + |
567 | 595 | /** |
568 | 596 | * @example |
569 | 597 | * $.formUtils.loadModules('date, security.dev'); |
|
592 | 620 | return; |
593 | 621 | } |
594 | 622 |
|
| 623 | + var hasLoadedAnyModule = false; |
| 624 | + |
595 | 625 | var loadModuleScripts = function(modules, path) { |
596 | 626 | var moduleList = $.split(modules), |
597 | 627 | numModules = moduleList.length, |
598 | 628 | moduleLoadedCallback = function() { |
599 | 629 | numModules--; |
600 | 630 | if( numModules == 0 ) { |
601 | 631 | $.formUtils.isLoadingModules = false; |
602 | | - if( fireEvent ) { |
| 632 | + if( fireEvent && hasLoadedAnyModule ) { |
603 | 633 | $.formUtils.trigger('load', path); |
604 | 634 | } |
605 | 635 | } |
|
620 | 650 | else { |
621 | 651 | var scriptUrl = path + modName + (modName.substr(-3) == '.js' ? '':'.js'), |
622 | 652 | script = document.createElement('SCRIPT'); |
623 | | - script.type = 'text/javascript'; |
624 | | - script.onload = moduleLoadedCallback; |
625 | | - script.src = scriptUrl + ( scriptUrl.substr(-7) == '.dev.js' ? cacheSuffix:'' ); |
626 | | - script.onreadystatechange = function() { |
627 | | - // IE 7 fix |
628 | | - if( this.readyState == 'complete' ) { |
629 | | - moduleLoadedCallback(); |
630 | | - } |
631 | | - }; |
632 | | - appendToElement.appendChild( script ); |
| 653 | + |
| 654 | + if( scriptUrl in $.formUtils.loadedModules ) { |
| 655 | + // already loaded |
| 656 | + moduleLoadedCallback(); |
| 657 | + } |
| 658 | + else { |
| 659 | + |
| 660 | + // Remember that this script is loaded |
| 661 | + $.formUtils.loadedModules[scriptUrl] = 1; |
| 662 | + hasLoadedAnyModule = true; |
| 663 | + |
| 664 | + // Load the script |
| 665 | + script.type = 'text/javascript'; |
| 666 | + script.onload = moduleLoadedCallback; |
| 667 | + script.src = scriptUrl + ( scriptUrl.substr(-7) == '.dev.js' ? cacheSuffix:'' ); |
| 668 | + script.onreadystatechange = function() { |
| 669 | + // IE 7 fix |
| 670 | + if( this.readyState == 'complete' ) { |
| 671 | + moduleLoadedCallback(); |
| 672 | + } |
| 673 | + }; |
| 674 | + appendToElement.appendChild( script ); |
| 675 | + } |
633 | 676 | } |
634 | 677 | }); |
635 | 678 | }; |
|
0 commit comments