|
12 | 12 | 'use strict'; |
13 | 13 |
|
14 | 14 | /** |
15 | | - * Should be called on the element containing the input elements |
| 15 | + * Assigns validateInputOnBlur function to elements blur event |
16 | 16 | * |
17 | 17 | * @param {Object} language Optional, will override $.formUtils.LANG |
18 | 18 | * @param {Object} settings Optional, will override the default settings |
19 | 19 | * @return {jQuery} |
20 | 20 | */ |
21 | 21 | $.fn.validateOnBlur = function(language, settings) { |
22 | | - this.find('textarea,input').blur(function() { |
23 | | - $(this).doValidate(language, settings); |
| 22 | + this.find('input[data-validation], textarea[data-validation]') |
| 23 | + .blur(function() { |
| 24 | + $(this).validateInputOnBlur(language, settings); |
24 | 25 | }); |
25 | | - |
26 | 26 | return this; |
27 | 27 | }; |
28 | 28 |
|
29 | 29 | /** |
30 | | - * Should be called on the element containing the input elements. |
| 30 | + * fade in help message when input gains focus |
| 31 | + * fade out when input loses focus |
31 | 32 | * <input data-help="The info that I want to display for the user when input is focused" ... /> |
32 | 33 | * |
33 | 34 | * @param {String} attrName - Optional, default is data-help |
|
71 | 72 | }; |
72 | 73 |
|
73 | 74 | /** |
74 | | - * Function that validates the value of given input and shows |
75 | | - * error message in a span element that is appended to the parent |
76 | | - * element |
| 75 | + * Validate single input when it loses focus |
| 76 | + * shows error message in a span element |
| 77 | + * that is appended to the parent element |
77 | 78 | * |
78 | 79 | * @param {Object} [language] Optional, will override $.formUtils.LANG |
79 | 80 | * @param {Object} [config] Optional, will override the default settings |
80 | 81 | * @param {Boolean} [attachKeyupEvent] Optional |
81 | 82 | * @return {jQuery} |
82 | 83 | */ |
83 | | - $.fn.doValidate = function(language, config, attachKeyupEvent) { |
| 84 | + $.fn.validateInputOnBlur = function(language, config, attachKeyupEvent) { |
84 | 85 | if(attachKeyupEvent === undefined) { |
85 | 86 | attachKeyupEvent = true; |
86 | 87 | } |
|
143 | 144 |
|
144 | 145 | if(attachKeyupEvent) { |
145 | 146 | $element.bind('keyup', function() { |
146 | | - $(this).doValidate(language, config, false); |
| 147 | + $(this).validateInputOnBlur(language, config, false); |
147 | 148 | }); |
148 | 149 | } |
149 | 150 | } |
|
177 | 178 | * @param [language] |
178 | 179 | * @param [config] |
179 | 180 | */ |
180 | | - $.fn.validate = function(language, config) { |
| 181 | + $.fn.validateForm = function(language, config) { |
181 | 182 |
|
182 | 183 | language = $.extend($.formUtils.LANG, language || {}); |
183 | 184 | config = $.extend($.formUtils.defaultConfig(), config || {}); |
|
410 | 411 | }, 200); |
411 | 412 | return false; |
412 | 413 | } |
413 | | - var valid = $(this).validate(config.language, config); |
| 414 | + var valid = $(this).validateForm(config.language, config); |
414 | 415 | if( valid && typeof config.onSuccess == 'function') { |
415 | 416 | var callbackResponse = config.onSuccess($form); |
416 | 417 | if( callbackResponse === false ) |
|
450 | 451 | $.formUtils = { |
451 | 452 |
|
452 | 453 | /** |
453 | | - * Default config for $(...).validate(); |
| 454 | + * Default config for $(...).validateForm(); |
454 | 455 | */ |
455 | 456 | defaultConfig : function() { |
456 | 457 | return { |
|
695 | 696 |
|
696 | 697 | var validator = $.formUtils.validators[rule]; |
697 | 698 |
|
698 | | - if( validator && typeof validator['validate'] == 'function' ) { |
| 699 | + if( validator && typeof validator['validatorFunction'] == 'function' ) { |
699 | 700 |
|
700 | | - var isValid = validator.validate(value, $element, config, language, $form); |
| 701 | + var isValid = validator.validatorFunction(value, $element, config, language, $form); |
701 | 702 |
|
702 | 703 | if(!isValid) { |
703 | 704 | validationErrorMsg = $element.attr(config.validationErrorMsgAttribute); |
|
1110 | 1111 | */ |
1111 | 1112 | $.formUtils.addValidator({ |
1112 | 1113 | name : 'email', |
1113 | | - validate : function(email) { |
| 1114 | + validatorFunction : function(email) { |
1114 | 1115 | var emailFilter = /^([a-zA-Z0-9_\.\-])+@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; |
1115 | 1116 | if(emailFilter.test(email)) { |
1116 | 1117 | var parts = email.split('@'); |
1117 | 1118 | if(parts.length == 2) { |
1118 | | - return $.formUtils.validators.validate_domain.validate(parts[1]); |
| 1119 | + return $.formUtils.validators.validate_domain.validatorFunction(parts[1]); |
1119 | 1120 | } |
1120 | 1121 | } |
1121 | 1122 | return false; |
|
1129 | 1130 | */ |
1130 | 1131 | $.formUtils.addValidator({ |
1131 | 1132 | name : 'domain', |
1132 | | - validate : function(val, $input) { |
| 1133 | + validatorFunction : function(val, $input) { |
1133 | 1134 |
|
1134 | 1135 | var topDomains = ['.com', '.net', '.org', '.biz', '.coop', '.info', '.museum', '.name', '.pro', |
1135 | 1136 | '.edu', '.gov', '.int', '.mil', '.ac', '.ad', '.ae', '.af', '.ag', '.ai', '.al', |
|
1220 | 1221 | */ |
1221 | 1222 | $.formUtils.addValidator({ |
1222 | 1223 | name : 'required', |
1223 | | - validate : function(val, $el) { |
| 1224 | + validatorFunction : function(val, $el) { |
1224 | 1225 | return $el.attr('type') == 'checkbox' ? $el.is(':checked') : $.trim(val) !== ''; |
1225 | 1226 | }, |
1226 | 1227 | errorMessage : '', |
|
1232 | 1233 | */ |
1233 | 1234 | $.formUtils.addValidator({ |
1234 | 1235 | name : 'length', |
1235 | | - validate : function(value, $el, config, lang) { |
| 1236 | + validatorFunction : function(value, $el, config, lang) { |
1236 | 1237 | var lengthAllowed = $el.valAttr('length'); |
1237 | 1238 | if(lengthAllowed == undefined) { |
1238 | 1239 | var elementType = $el.get(0).nodeName; |
|
1275 | 1276 | */ |
1276 | 1277 | $.formUtils.addValidator({ |
1277 | 1278 | name : 'url', |
1278 | | - validate : function(url) { |
| 1279 | + validatorFunction : function(url) { |
1279 | 1280 | // written by Scott Gonzalez: http://projects.scottsplayground.com/iri/ but added support for arrays in the url ?arg[]=sdfsdf |
1280 | 1281 | var urlFilter = /^(https|http|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|\[|\]|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i; |
1281 | 1282 | if( urlFilter.test(url) ) { |
|
1284 | 1285 | if(domainSlashPos > -1) |
1285 | 1286 | domain = domain.substr(0, domainSlashPos); |
1286 | 1287 |
|
1287 | | - return $.formUtils.validators.validate_domain.validate(domain); // todo: add support for IP-addresses |
| 1288 | + return $.formUtils.validators.validate_domain.validatorFunction(domain); // todo: add support for IP-addresses |
1288 | 1289 | } |
1289 | 1290 | return false; |
1290 | 1291 | }, |
|
1297 | 1298 | */ |
1298 | 1299 | $.formUtils.addValidator({ |
1299 | 1300 | name : 'number', |
1300 | | - validate : function(val, $el) { |
| 1301 | + validatorFunction : function(val, $el) { |
1301 | 1302 | if(val !== '') { |
1302 | 1303 | var allowing = $el.valAttr('allowing') || ''; |
1303 | 1304 | if(allowing.indexOf('number') == -1) |
|
1325 | 1326 | */ |
1326 | 1327 | $.formUtils.addValidator({ |
1327 | 1328 | name : 'alphanumeric', |
1328 | | - validate : function(val, $el, config, language) { |
| 1329 | + validatorFunction : function(val, $el, config, language) { |
1329 | 1330 | var patternStart = '^([a-zA-Z0-9', |
1330 | 1331 | patternEnd = ']+)$', |
1331 | 1332 | additionalChars = $el.attr('data-validation-allowing'), |
|
1355 | 1356 | */ |
1356 | 1357 | $.formUtils.addValidator({ |
1357 | 1358 | name : 'custom', |
1358 | | - validate : function(val, $el, config) { |
| 1359 | + validatorFunction : function(val, $el, config) { |
1359 | 1360 | var regexp = new RegExp($el.valAttr('regexp')); |
1360 | 1361 | return regexp.test(val); |
1361 | 1362 | }, |
|
1368 | 1369 | */ |
1369 | 1370 | $.formUtils.addValidator({ |
1370 | 1371 | name : 'date', |
1371 | | - validate : function(date, $el, conf) { |
| 1372 | + validatorFunction : function(date, $el, conf) { |
1372 | 1373 | var dateFormat = 'yyyy-mm-dd'; |
1373 | 1374 | if($el.valAttr('format')) { |
1374 | 1375 | dateFormat = $el.valAttr('format'); |
|
1400 | 1401 |
|
1401 | 1402 | $.formUtils.addValidator({ |
1402 | 1403 | name : 'checkbox_group', |
1403 | | - validate : function(val, $el, config, lang, form) |
| 1404 | + validatorFunction : function(val, $el, config, lang, form) |
1404 | 1405 | { // set return var |
1405 | 1406 | var checkResult = true; |
1406 | 1407 | // get name of element. since it is a checkbox group, all checkboxes will have same name |
|
0 commit comments