|
5 | 5 | * Documentation and issue tracking on Github <https://github.com/victorjonsson/jQuery-Form-Validator/>
|
6 | 6 | *
|
7 | 7 | * @license Dual licensed under the MIT or GPL Version 2 licenses
|
8 |
| -* @version 1.9.13 |
| 8 | +* @version 1.9.15 |
9 | 9 | */
|
10 | 10 | (function($) {
|
11 | 11 |
|
|
130 | 130 | validateWhenBlurred($element);
|
131 | 131 | }
|
132 | 132 |
|
133 |
| - var validation = $.formUtils.validateInput($element, language, config); |
| 133 | + var validation = $.formUtils.validateInput($element, language, config, $form); |
134 | 134 |
|
135 | 135 | if(validation === true) {
|
136 | 136 | $element.addClass('valid');
|
|
264 | 264 | var valid = $.formUtils.validateInput(
|
265 | 265 | $element,
|
266 | 266 | language,
|
267 |
| - config |
| 267 | + config, |
| 268 | + $form |
268 | 269 | );
|
269 | 270 |
|
270 | 271 | if(valid !== true) {
|
|
298 | 299 | //
|
299 | 300 | // Remove possible error messages from last validation
|
300 | 301 | //
|
301 |
| - $('.' + config.errorMessageClass.split(' ').join('.')).remove(); |
| 302 | + $('.' + $.split(config.errorMessageClass, ' ').join('.')).remove(); |
302 | 303 | $('.jquery_form_error_message').remove();
|
303 | 304 |
|
304 | 305 |
|
|
354 | 355 | return this;
|
355 | 356 | };
|
356 | 357 |
|
| 358 | + /** |
| 359 | + * Add suggestion dropdown to inputs having data-suggestions with a comma |
| 360 | + * separated string with suggestions |
| 361 | + * @param {Array} [settings] |
| 362 | + * @returns {jQuery} |
| 363 | + */ |
| 364 | + $.fn.addSuggestions = function(settings) { |
| 365 | + this.find('input').each(function() { |
| 366 | + var $input = $(this), |
| 367 | + suggestions = $input.attr('data-suggestions'); |
| 368 | + |
| 369 | + if( suggestions && suggestions.length ) { |
| 370 | + $.formUtils.suggest($input, $.split(suggestions), settings); |
| 371 | + } |
| 372 | + }); |
| 373 | + return this; |
| 374 | + }; |
| 375 | + |
| 376 | + /** |
| 377 | + * A bit smarter split function |
| 378 | + * @param {String} val |
| 379 | + * @param {Function|String} [func] |
| 380 | + * @param {String} [delim] |
| 381 | + * @returns {Array|void} |
| 382 | + */ |
| 383 | + $.split = function(val, func, delim) { |
| 384 | + if( typeof func != 'function' ) { |
| 385 | + // return string |
| 386 | + if( !val ) |
| 387 | + return []; |
| 388 | + var values = []; |
| 389 | + $.each(val.split(func ? func:','), function(i,str) { |
| 390 | + str = $.trim(str); |
| 391 | + if( str.length ) |
| 392 | + values.push(str); |
| 393 | + }); |
| 394 | + return values; |
| 395 | + } else if( val ) { |
| 396 | + // use callback on each |
| 397 | + if( !delim ) |
| 398 | + delim = ','; |
| 399 | + $.each(val.split(delim), function(i, str) { |
| 400 | + str = $.trim(str); |
| 401 | + if( str.length ) |
| 402 | + return func(str, i); |
| 403 | + }); |
| 404 | + } |
| 405 | + }; |
357 | 406 |
|
358 | 407 | $.formUtils = {
|
359 | 408 |
|
|
453 | 502 | loadModules : function(modules, path) {
|
454 | 503 |
|
455 | 504 | var loadModuleScripts = function(modules, path) {
|
456 |
| - var moduleList = modules.split(','); |
457 |
| - var numModules = moduleList.length; |
458 |
| - var moduleLoadedCallback = function() { |
459 |
| - numModules--; |
460 |
| - if( numModules == 0 ) { |
461 |
| - $.formUtils.trigger('load', path); |
462 |
| - } |
463 |
| - }; |
464 |
| - $.each(moduleList, function(i, module) { |
465 |
| - var moduleName = $.trim(module); |
466 |
| - if( moduleName.length == 0 ) { |
| 505 | + var moduleList = $.split(modules), |
| 506 | + numModules = moduleList.length, |
| 507 | + moduleLoadedCallback = function() { |
| 508 | + numModules--; |
| 509 | + if( numModules == 0 ) { |
| 510 | + $.formUtils.trigger('load', path); |
| 511 | + } |
| 512 | + }; |
| 513 | + |
| 514 | + $.each(moduleList, function(i, modName) { |
| 515 | + modName = $.trim(modName); |
| 516 | + if( modName.length == 0 ) { |
467 | 517 | moduleLoadedCallback();
|
468 | 518 | }
|
469 | 519 | else {
|
470 |
| - var scriptUrl = path + $.trim(module) + '.js'; |
| 520 | + var scriptUrl = path + modName + (modName.substr(-3) == '.js' ? '':'.js'); |
471 | 521 | $.ajax({
|
472 | 522 | url : scriptUrl,
|
473 | 523 | cache : scriptUrl.substr(-7) != '.dev.js',
|
|
478 | 528 | },
|
479 | 529 | error : function() {
|
480 | 530 | moduleLoadedCallback();
|
481 |
| - throw new Error('Unable to load form validation module '+module); |
| 531 | + throw new Error('Unable to load form validation module '+modName); |
482 | 532 | }
|
483 | 533 | });
|
484 | 534 | }
|
|
491 | 541 | $(function() {
|
492 | 542 | $('script').each(function() {
|
493 | 543 | var src = $(this).attr('src');
|
494 |
| - var scriptName = src.substr(src.lastIndexOf('/')+1, src.length); |
495 |
| - if(scriptName == 'jquery.form-validator.js' || scriptName == 'jquery.form-validator.min.js') { |
496 |
| - path = src.substr(0, src.lastIndexOf('/')) + '/'; |
497 |
| - if(path == '/') |
498 |
| - path = ''; |
| 544 | + if( src ) { |
| 545 | + var scriptName = src.substr(src.lastIndexOf('/')+1, src.length); |
| 546 | + if(scriptName.indexOf('jquery.form-validator.js') > -1 || scriptName.indexOf('jquery.form-validator.min.js') > -1) { |
| 547 | + path = src.substr(0, src.lastIndexOf('/')) + '/'; |
| 548 | + if(path == '/') |
| 549 | + path = ''; |
499 | 550 |
|
500 |
| - return false; |
| 551 | + return false; |
| 552 | + } |
501 | 553 | }
|
502 | 554 | });
|
503 | 555 |
|
|
514 | 566 | * @param {jQuery} $element
|
515 | 567 | * @param {Object} language ($.formUtils.LANG)
|
516 | 568 | * @param {Object} config
|
| 569 | + * @param {jQuery} $form |
517 | 570 | * @return {String|Boolean}
|
518 | 571 | */
|
519 |
| - validateInput : function($element, language, config) { |
| 572 | + validateInput : function($element, language, config, $form) { |
520 | 573 |
|
521 | 574 | // Multiple select
|
522 | 575 | if( $element.get(0).nodeName == 'SELECT' && $element.attr('multiple') ) {
|
|
560 | 613 | var validationRules = $element.attr(config.validationRuleAttribute);
|
561 | 614 |
|
562 | 615 | // see if form element has inline err msg attribute
|
563 |
| - var validationErrorMsg = $element.attr(config.validationErrorMsgAttribute); |
| 616 | + var validationErrorMsg = true; |
564 | 617 |
|
565 |
| - if ( validationRules ) { |
566 |
| - var posRules = validationRules.split(' '); |
567 |
| - for(var i=0; i < posRules.length; i++) { |
568 |
| - if( posRules[i].substr(0, 9) != 'validate_' ) { |
569 |
| - posRules[i] = 'validate_' + posRules[i]; |
570 |
| - } |
| 618 | + $.split(validationRules, function(rule) { |
| 619 | + if( rule.indexOf('validate_') !== 0 ) { |
| 620 | + rule = 'validate_' + rule; |
| 621 | + } |
571 | 622 |
|
572 |
| - var validator = $.formUtils.validators[posRules[i]]; |
| 623 | + var validator = $.formUtils.validators[rule]; |
573 | 624 |
|
574 |
| - if( validator && typeof validator['validate'] == 'function' ) { |
| 625 | + if( validator && typeof validator['validate'] == 'function' ) { |
575 | 626 |
|
576 |
| - var isValid = validator.validate(value, $element, config, language, $element.closest('form')); |
| 627 | + var isValid = validator.validate(value, $element, config, language, $form); |
577 | 628 |
|
578 |
| - if(!isValid) { |
579 |
| - $.formUtils.trigger('invalid', $element); |
580 |
| - if( !validationErrorMsg ) { |
581 |
| - validationErrorMsg = language[validator.errorMessageKey]; |
582 |
| - if(typeof validationErrorMsg == 'undefined') |
583 |
| - validationErrorMsg = validator.errorMessage; |
584 |
| - } |
585 |
| - return validationErrorMsg; |
| 629 | + if(!isValid) { |
| 630 | + $.formUtils.trigger('invalid', $element); |
| 631 | + validationErrorMsg = $element.attr(config.validationErrorMsgAttribute); |
| 632 | + if( !validationErrorMsg ) { |
| 633 | + validationErrorMsg = language[validator.errorMessageKey]; |
| 634 | + if( !validationErrorMsg ) |
| 635 | + validationErrorMsg = validator.errorMessage; |
586 | 636 | }
|
587 |
| - } else { |
588 |
| - console.warn('Using undefined validator "'+posRules[i]+'"'); |
| 637 | + return false; // breaks the iteration |
589 | 638 | }
|
| 639 | + } else { |
| 640 | + console.warn('Using undefined validator "'+rule+'"'); |
590 | 641 | }
|
| 642 | + |
| 643 | + }, ' '); |
| 644 | + |
| 645 | + if( typeof validationErrorMsg == 'string' ) { |
| 646 | + return validationErrorMsg; |
| 647 | + } else { |
| 648 | + $.formUtils.trigger('valid', $element); |
| 649 | + return true; |
591 | 650 | }
|
592 |
| - $.formUtils.trigger('valid', $element); |
593 |
| - return true; |
594 | 651 | },
|
595 | 652 |
|
596 | 653 | /**
|
|
1074 | 1131 | */
|
1075 | 1132 | $.formUtils.addValidator({
|
1076 | 1133 | name : 'validate_required',
|
1077 |
| - validate : function(val) { |
1078 |
| - return val !== ''; |
| 1134 | + validate : function(val, $el) { |
| 1135 | + return $el.attr('type') == 'checkbox' ? $el.is(':checked') : $.trim(val) !== ''; |
1079 | 1136 | },
|
1080 | 1137 | errorMessage : '',
|
1081 | 1138 | errorMessageKey: 'requiredFields'
|
|
1094 | 1151 | return true;
|
1095 | 1152 | }
|
1096 | 1153 |
|
1097 |
| - var range = len.split('-'); |
| 1154 | + var range = $.split(len, '-'); |
1098 | 1155 |
|
1099 | 1156 | // range
|
1100 | 1157 | if(range.length == 2 && (value.length < parseInt(range[0],10) || value.length > parseInt(range[1],10))) {
|
|
0 commit comments