Skip to content

Commit 689268f

Browse files
committed
manually merge pull request made by coffein, added $.setupForm
1 parent c2c9b92 commit 689268f

File tree

10 files changed

+179
-136
lines changed

10 files changed

+179
-136
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ So what has changed since version 1.x?
4545
* A modular design have been introduced, which means that some validation functions is default and others is
4646
part of a module.
4747
* You no longer need to prefix the validation rules with "validate_"
48+
* Error message position now defaults to "element"
4849

4950

5051
### Default validators and features (no module needed)
@@ -228,8 +229,8 @@ var myConf = {
228229
// Position of error messages. Set the value to "top" if you want the error messages
229230
// to be displayed in the top of the form. Otherwise you can set the value to
230231
// "element", each error message will then be displayed beside the input field that
231-
// it is refering to (default is 'top')
232-
errorMessagePosition : 'element',
232+
// it is refering to (default is 'element')
233+
errorMessagePosition : 'top',
233234

234235
// Date format used when validating dates and birthdate. (default is yyyy-mm-dd)
235236
dateFormat : 'dd/mm/yyyy',

form-validator/date.dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*
1313
*
1414
* @license Dual licensed under the MIT or GPL Version 2 licenses
15-
* @version 1.9.32
15+
* @version 1.9.33
1616
*/
1717
(function($) {
1818

form-validator/jquery.form-validator.js

Lines changed: 122 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Documentation and issue tracking on Github <https://github.com/victorjonsson/jQuery-Form-Validator/>
66
*
77
* @license Dual licensed under the MIT or GPL Version 2 licenses
8-
* @version 1.9.32
8+
* @version 1.9.33
99
*/
1010
(function($) {
1111

@@ -187,7 +187,7 @@
187187
*/
188188
$.fn.validate = function(language, config) {
189189

190-
language = $.extend(language || {}, $.formUtils.LANG);
190+
language = $.extend($.formUtils.LANG, language || {});
191191
config = $.extend($.formUtils.defaultConfig(), config || {});
192192

193193
/**
@@ -233,53 +233,27 @@
233233
var elementType = $element.attr('type');
234234
if (!ignoreInput($element.attr('name'), elementType)) {
235235

236-
// input of type radio
237-
if(elementType === 'radio') {
238-
var validationRule = $element.attr(config.validationRuleAttribute);
239-
if (typeof validationRule != 'undefined' && validationRule === 'required') {
240-
var radioButtonName = $element.attr('name');
241-
var isChecked = false;
242-
$form.find('input[name="' + radioButtonName + '"]').each(function() {
243-
if ($(this).is(':checked')) {
244-
isChecked = true;
245-
return false;
246-
}
247-
return true;
248-
});
249-
250-
if (!isChecked) {
251-
var validationErrorMsg = $element.attr(config.validationErrorMsgAttribute);
252-
$element.valAttr('current-error', validationErrorMsg || language.requiredFields);
253-
errorMessages.push(validationErrorMsg || language.requiredFields);
254-
errorInputs.push($element);
255-
}
256-
}
236+
// memorize border color
237+
$.formUtils.figureOutDefaultBorderColor($element);
238+
239+
var validation = $.formUtils.validateInput(
240+
$element,
241+
language,
242+
config,
243+
$form
244+
);
245+
246+
if(validation !== true) {
247+
errorInputs.push($element);
248+
addErrorMessage(validation);
249+
$element
250+
.valAttr('current-error', validation)
251+
.removeClass('valid');
257252
}
258-
// inputs, textareas and select lists
259253
else {
260-
261-
// memorize border color
262-
$.formUtils.figureOutDefaultBorderColor($element);
263-
264-
var valid = $.formUtils.validateInput(
265-
$element,
266-
language,
267-
config,
268-
$form
269-
);
270-
271-
if(valid !== true) {
272-
errorInputs.push($element);
273-
addErrorMessage(valid);
274-
$element
275-
.valAttr('current-error', valid)
276-
.removeClass('valid');
277-
}
278-
else {
279-
$element
280-
.valAttr('current-error', false)
281-
.addClass('valid');
282-
}
254+
$element
255+
.valAttr('current-error', false)
256+
.addClass('valid');
283257
}
284258
}
285259

@@ -406,6 +380,74 @@
406380
}
407381
};
408382

383+
/**
384+
* Short hand function that makes the validation setup require less code
385+
* @param config
386+
*/
387+
$.setupForm = function(config) {
388+
config = $.extend({
389+
form : 'form',
390+
validateOnBlur : true,
391+
showHelpOnFocus : true,
392+
addSuggestions : true,
393+
modules : '',
394+
onModulesLoaded : null,
395+
language : false,
396+
onSuccess : false,
397+
onError : false
398+
}, config || {});
399+
400+
$.split(config.form, function(formQuery) {
401+
var $form = $(formQuery);
402+
403+
// Validate when submitted
404+
$form.bind('submit', function() {
405+
if($.formUtils.isLoadingModules) {
406+
setTimeout(function() {
407+
$form.trigger('submit');
408+
}, 200);
409+
return false;
410+
}
411+
var valid = $(this).validate(config.language, config);
412+
if( valid && typeof config.onSuccess == 'function') {
413+
var callbackResponse = config.onSuccess($form);
414+
if( callbackResponse === false )
415+
return false;
416+
} else if ( !valid && typeof config.onError == 'function' ) {
417+
config.onError($form);
418+
return false;
419+
} else {
420+
return valid;
421+
}
422+
});
423+
424+
if( config.validateOnBlur ) {
425+
$form.validateOnBlur(config.language, config);
426+
}
427+
if( config.showHelpOnFocus ) {
428+
$form.showHelpOnFocus();
429+
}
430+
431+
if( config.addSuggestions ) {
432+
$form.addSuggestions();
433+
}
434+
});
435+
436+
if( config.modules != '' ) {
437+
if( typeof config.onModulesLoaded == 'function' ) {
438+
$.formUtils.on('load', function() {
439+
$.split(config.form, function(formQuery) {
440+
config.onModulesLoaded( $(formQuery) );
441+
});
442+
});
443+
}
444+
$.formUtils.loadModules(config.modules);
445+
}
446+
};
447+
448+
/**
449+
* Object containing utility methods for this plugin
450+
*/
409451
$.formUtils = {
410452

411453
/**
@@ -419,7 +461,7 @@
419461
errorMessageClass : 'jquery_form_error_message', // class name of div containing error messages when validation fails
420462
validationRuleAttribute : 'data-validation', // name of the attribute holding the validation rules
421463
validationErrorMsgAttribute : 'data-validation-error-msg', // define custom err msg inline with element
422-
errorMessagePosition : 'top', // Can be either "top" or "element"
464+
errorMessagePosition : 'element', // Can be either "top" or "element"
423465
scrollToTopOnError : true,
424466
dateFormat : 'yyyy-mm-dd'
425467
}
@@ -487,6 +529,11 @@
487529
});
488530
},
489531

532+
/**
533+
* @ {Boolean}
534+
*/
535+
isLoadingModules : false,
536+
490537
/**
491538
* @example
492539
* $.formUtils.loadModules('date, security.dev');
@@ -510,10 +557,15 @@
510557
moduleLoadedCallback = function() {
511558
numModules--;
512559
if( numModules == 0 ) {
560+
$.formUtils.isLoadingModules = false;
513561
$.formUtils.trigger('load', path);
514562
}
515563
};
516564

565+
if( numModules > 0 ) {
566+
$.formUtils.isLoadingModules = true;
567+
}
568+
517569
$.each(moduleList, function(i, modName) {
518570
modName = $.trim(modName);
519571
if( modName.length == 0 ) {
@@ -581,13 +633,7 @@
581633
*/
582634
validateInput : function($element, language, config, $form) {
583635

584-
// Multiple select
585-
if( $element.get(0).nodeName == 'SELECT' && $element.attr('multiple') ) {
586-
return this.validateMultipleSelect($element.val(), $element, config, language);
587-
}
588-
589-
var value = $.trim($element.val());
590-
value = value || '';
636+
var value = $.trim( $element.val() || '' );
591637
var optional = $element.valAttr('optional');
592638

593639
// test if a checkbox forces this element to be validated
@@ -660,26 +706,6 @@
660706
}
661707
},
662708

663-
/**
664-
* @param {Array} values
665-
* @param {jQuery} $el
666-
* @param {Object} config
667-
* @param {Object} language - $.formUtils.LANG
668-
* @return {Boolean|String}
669-
*/
670-
validateMultipleSelect : function(values, $el, config, language) {
671-
values = values || [];
672-
var validationRules = $el.attr(config.validationRuleAttribute);
673-
var validationErrorMsg = $el.attr(config.validationErrorMsgAttribute);
674-
if(validationRules.indexOf('validate_num_answers') > -1) {
675-
var num = this.getAttributeInteger(validationRules, 'num');
676-
if(num > values.length) {
677-
return validationErrorMsg || (language.badNumberOfSelectedOptionsStart +num+ language.badNumberOfSelectedOptionsEnd);
678-
}
679-
}
680-
return true;
681-
},
682-
683709
/**
684710
* <input data-validation="validate_min_length length12" /> => getAttribute($(element).attr('data-validation'), 'length') = 12
685711
* @param {String} attrValue
@@ -769,27 +795,31 @@
769795
* @return void
770796
*/
771797
lengthRestriction : function($inputElement, $maxLengthElement) {
772-
// read maxChars from counter display initial text value
773-
var maxChars = parseInt($maxLengthElement.text(),10);
798+
// read maxChars from counter display initial text value
799+
var maxChars = parseInt($maxLengthElement.text(),10),
800+
801+
// internal function does the counting and sets display value
802+
countCharacters = function() {
803+
var numChars = $inputElement.val().length;
804+
if(numChars > maxChars) {
805+
// get current scroll bar position
806+
var currScrollTopPos = $inputElement.scrollTop();
807+
// trim value to max length
808+
$inputElement.val($inputElement.val().substring(0, maxChars));
809+
$inputElement.scrollTop(currScrollTopPos);
810+
}
811+
// set counter text
812+
$maxLengthElement.text(maxChars - numChars);
813+
};
774814

775815
// bind events to this element
776816
// setTimeout is needed, cut or paste fires before val is available
777817
$($inputElement).bind('keydown keyup keypress focus blur', countCharacters )
778818
.bind('cut paste', function(){ setTimeout(countCharacters, 100); } ) ;
779819

780-
// internal function does the counting and sets display value
781-
function countCharacters() {
782-
var numChars = $inputElement.val().length;
783-
if(numChars > maxChars) {
784-
// get current scroll bar position
785-
var currScrollTopPos = $inputElement.scrollTop();
786-
// trim value to max length
787-
$inputElement.val($inputElement.val().substring(0, maxChars));
788-
$inputElement.scrollTop(currScrollTopPos);
789-
}
790-
// set counter text
791-
$maxLengthElement.text(maxChars - numChars);
792-
}
820+
// count chars on pageload, if there are prefilled input-values
821+
$(document).bind("ready", countCharacters);
822+
793823
},
794824

795825
_numSuggestionElements : 0,
@@ -1056,12 +1086,6 @@
10561086
name : 'validate_domain',
10571087
validate : function(val, $input) {
10581088

1059-
// Clean up
1060-
val = val.toLowerCase();
1061-
val = val.replace('ftp://', '').replace('https://', '').replace('http://', '').replace('www.', '');
1062-
if(val.substr(-1) == '/')
1063-
val = val.substr(0, val.length-1);
1064-
10651089
var topDomains = ['.com', '.net', '.org', '.biz', '.coop', '.info', '.museum', '.name', '.pro',
10661090
'.edu', '.gov', '.int', '.mil', '.ac', '.ad', '.ae', '.af', '.ag', '.ai', '.al',
10671091
'.am', '.an', '.ao', '.aq', '.ar', '.as', '.at', '.au', '.aw', '.az', '.ba', '.bb',
@@ -1209,8 +1233,8 @@
12091233
validate : function(url) {
12101234
// written by Scott Gonzalez: http://projects.scottsplayground.com/iri/ but added support for arrays in the url ?arg[]=sdfsdf
12111235
var urlFilter = /^(https|http|ftp):\/\/(((([a-z]|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F])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i;
1212-
if(urlFilter.test(url)) {
1213-
var domain = url.split(/^https|^http|^ftp/i)[1].replace('://', '');
1236+
if( urlFilter.test(url) ) {
1237+
var domain = url.split('://')[1];
12141238
var domainSlashPos = domain.indexOf('/');
12151239
if(domainSlashPos > -1)
12161240
domain = domain.substr(0, domainSlashPos);

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
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* @license Dual licensed under the MIT or GPL Version 2 licenses
14-
* @version 1.9.32
14+
* @version 1.9.33
1515
*/
1616
(function($) {
1717

0 commit comments

Comments
 (0)