|
4 | 4 | * Licensed under the MIT license |
5 | 5 | * |
6 | 6 | * @author Tom Bertrand |
7 | | - * @version 2.0.0 (2015-04-22) |
| 7 | + * @version 1.5.3 (2015-06-14) |
8 | 8 | * @link http://www.runningcoder.org/jqueryvalidation/ |
9 | 9 | */ |
10 | 10 | ; |
|
22 | 22 | }; |
23 | 23 | } |
24 | 24 | var _rules = { |
25 | | - NOTEMPTY: /\S/, |
26 | | - INTEGER: /^\d+$/, |
27 | | - NUMERIC: /^\d+(?:[,\s]\d{3})*(?:\.\d+)?$/, |
28 | | - MIXED: /^[\w\s-]+$/, |
29 | | - NAME: /^['a-zãàáäâẽèéëêìíïîõòóöôùúüûñç\s-]+$/i, |
30 | | - NOSPACE: /^(?!\s)\S*$/, |
31 | | - TRIM: /^[^\s].*[^\s]$/, |
32 | | - DATE: /^\d{4}-\d{2}-\d{2}(\s\d{2}:\d{2}(:\d{2})?)?$/, |
33 | | - EMAIL: /^([^@]+?)@(([a-z0-9]-*)*[a-z0-9]+\.)+([a-z0-9]+)$/i, |
34 | | - URL: /^(https?:\/\/)?((([a-z0-9]-*)*[a-z0-9]+\.?)*([a-z0-9]+))(\/[\w?=\.-]*)*$/, |
35 | | - PHONE: /^(\()?\d{3}(\))?(-|\s)?\d{3}(-|\s)\d{4}$/, |
36 | | - OPTIONAL: /\S/, |
37 | | - COMPARISON: /^\s*([LV])\s*([<>]=?|==|!=)\s*([^<>=!]+?)\s*$/ |
38 | | - }, |
| 25 | + NOTEMPTY: /\S/, |
| 26 | + INTEGER: /^\d+$/, |
| 27 | + NUMERIC: /^\d+(?:[,\s]\d{3})*(?:\.\d+)?$/, |
| 28 | + MIXED: /^[\w\s-]+$/, |
| 29 | + NAME: /^['a-zãàáäâẽèéëêìíïîõòóöôùúüûñç\s-]+$/i, |
| 30 | + NOSPACE: /^(?!\s)\S*$/, |
| 31 | + TRIM: /^[^\s].*[^\s]$/, |
| 32 | + DATE: /^\d{4}-\d{2}-\d{2}(\s\d{2}:\d{2}(:\d{2})?)?$/, |
| 33 | + EMAIL: /^([^@]+?)@(([a-z0-9]-*)*[a-z0-9]+\.)+([a-z0-9]+)$/i, |
| 34 | + URL: /^(https?:\/\/)?((([a-z0-9]-*)*[a-z0-9]+\.?)*([a-z0-9]+))(\/[\w?=\.-]*)*$/, |
| 35 | + PHONE: /^(\()?\d{3}(\))?(-|\s)?\d{3}(-|\s)\d{4}$/, |
| 36 | + OPTIONAL: /\S/, |
| 37 | + COMPARISON: /^\s*([LV])\s*([<>]=?|==|!=)\s*([^<>=!]+?)\s*$/ |
| 38 | + }; |
39 | 39 |
|
40 | | - _messages = { |
41 | | - 'default': '$ contain error(s).', |
42 | | - 'NOTEMPTY': '$ must not be empty.', |
43 | | - 'INTEGER': '$ must be an integer.', |
44 | | - 'NUMERIC': '$ must be numeric.', |
45 | | - 'MIXED': '$ must be letters or numbers (no special characters).', |
46 | | - 'NAME': '$ must not contain special characters.', |
47 | | - 'NOSPACE': '$ must not contain spaces.', |
48 | | - 'TRIM': '$ must not start or end with space character.', |
49 | | - 'DATE': '$ is not a valid with format YYYY-MM-DD.', |
50 | | - 'EMAIL': '$ is not valid.', |
51 | | - 'URL': '$ is not valid.', |
52 | | - 'PHONE': '$ is not a valid phone number.', |
53 | | - '<': '$ must be less than % characters.', |
54 | | - '<=': '$ must be less or equal to % characters.', |
55 | | - '>': '$ must be greater than % characters.', |
56 | | - '>=': '$ must be greater or equal to % characters.', |
57 | | - '==': '$ must be equal to %', |
58 | | - '!=': '$ must be different than %' |
59 | | - }, |
| 40 | + var _messages = { |
| 41 | + 'default': '$ contain error(s).', |
| 42 | + 'NOTEMPTY': '$ must not be empty.', |
| 43 | + 'INTEGER': '$ must be an integer.', |
| 44 | + 'NUMERIC': '$ must be numeric.', |
| 45 | + 'MIXED': '$ must be letters or numbers (no special characters).', |
| 46 | + 'NAME': '$ must not contain special characters.', |
| 47 | + 'NOSPACE': '$ must not contain spaces.', |
| 48 | + 'TRIM': '$ must not start or end with space character.', |
| 49 | + 'DATE': '$ is not a valid with format YYYY-MM-DD.', |
| 50 | + 'EMAIL': '$ is not valid.', |
| 51 | + 'URL': '$ is not valid.', |
| 52 | + 'PHONE': '$ is not a valid phone number.', |
| 53 | + '<': '$ must be less than % characters.', |
| 54 | + '<=': '$ must be less or equal to % characters.', |
| 55 | + '>': '$ must be greater than % characters.', |
| 56 | + '>=': '$ must be greater or equal to % characters.', |
| 57 | + '==': '$ must be equal to %', |
| 58 | + '!=': '$ must be different than %' |
| 59 | + }; |
60 | 60 |
|
61 | | - _data = { |
62 | | - validation: 'data-validation', |
63 | | - validationMessage: 'data-validation-message', |
64 | | - regex: 'data-validation-regex', |
65 | | - regexReverse: 'data-validation-regex-reverse', |
66 | | - regexMessage: 'data-validation-regex-message', |
67 | | - group: 'data-validation-group', |
68 | | - label: 'data-validation-label', |
69 | | - errorList: 'data-error-list' |
70 | | - }, |
| 61 | + var _data = { |
| 62 | + validation: 'data-validation', |
| 63 | + validationMessage: 'data-validation-message', |
| 64 | + regex: 'data-validation-regex', |
| 65 | + regexReverse: 'data-validation-regex-reverse', |
| 66 | + regexMessage: 'data-validation-regex-message', |
| 67 | + group: 'data-validation-group', |
| 68 | + label: 'data-validation-label', |
| 69 | + errorList: 'data-error-list' |
| 70 | + } |
71 | 71 |
|
72 | | - _options = { |
73 | | - submit: { |
74 | | - settings: { |
75 | | - form: null, |
76 | | - display: "inline", |
77 | | - insertion: "append", |
78 | | - allErrors: false, |
79 | | - trigger: "click", |
80 | | - button: "[type='submit']", |
81 | | - errorClass: "error", |
82 | | - errorListClass: "error-list", |
83 | | - errorListContainer: null, |
84 | | - inputContainer: null, |
85 | | - clear: "focusin", |
86 | | - scrollToError: false |
87 | | - }, |
88 | | - callback: { |
89 | | - onInit: null, |
90 | | - onValidate: null, |
91 | | - onError: null, |
92 | | - onBeforeSubmit: null, |
93 | | - onSubmit: null, |
94 | | - onAfterSubmit: null |
95 | | - } |
| 72 | + var _options = { |
| 73 | + submit: { |
| 74 | + settings: { |
| 75 | + form: null, |
| 76 | + display: "inline", |
| 77 | + insertion: "append", |
| 78 | + allErrors: false, |
| 79 | + trigger: "click", |
| 80 | + button: "[type='submit']", |
| 81 | + errorClass: "error", |
| 82 | + errorListClass: "error-list", |
| 83 | + errorListContainer: null, |
| 84 | + inputContainer: null, |
| 85 | + clear: "focusin", |
| 86 | + scrollToError: false |
96 | 87 | }, |
97 | | - dynamic: { |
98 | | - settings: { |
99 | | - trigger: null, |
100 | | - delay: 300 |
101 | | - }, |
102 | | - callback: { |
103 | | - onSuccess: null, |
104 | | - onError: null, |
105 | | - onComplete: null |
106 | | - } |
| 88 | + callback: { |
| 89 | + onInit: null, |
| 90 | + onValidate: null, |
| 91 | + onError: null, |
| 92 | + onBeforeSubmit: null, |
| 93 | + onSubmit: null, |
| 94 | + onAfterSubmit: null |
| 95 | + } |
| 96 | + }, |
| 97 | + dynamic: { |
| 98 | + settings: { |
| 99 | + trigger: null, |
| 100 | + delay: 300 |
107 | 101 | }, |
108 | | - rules: {}, |
109 | | - messages: {}, |
110 | | - labels: {}, |
111 | | - debug: false |
| 102 | + callback: { |
| 103 | + onSuccess: null, |
| 104 | + onError: null, |
| 105 | + onComplete: null |
| 106 | + } |
112 | 107 | }, |
| 108 | + rules: {}, |
| 109 | + messages: {}, |
| 110 | + labels: {}, |
| 111 | + debug: false |
| 112 | + }; |
113 | 113 |
|
114 | | - _supported = { |
115 | | - submit: { |
116 | | - settings: { |
117 | | - display: ["inline", "block"], |
118 | | - insertion: ["append", "prepend"], //"before", "insertBefore", "after", "insertAfter" |
119 | | - allErrors: [true, false], |
120 | | - clear: ["focusin", "keypress", false], |
121 | | - trigger: [ |
122 | | - "click", "dblclick", "focusout", |
123 | | - "hover", "mousedown", "mouseenter", |
124 | | - "mouseleave", "mousemove", "mouseout", |
125 | | - "mouseover", "mouseup", "toggle" |
126 | | - ] |
127 | | - } |
128 | | - }, |
129 | | - dynamic: { |
130 | | - settings: { |
131 | | - trigger: ["focusout", "keydown", "keypress", "keyup"] |
132 | | - } |
133 | | - }, |
134 | | - debug: [true, false] |
135 | | - }; |
| 114 | + var _supported = { |
| 115 | + submit: { |
| 116 | + settings: { |
| 117 | + display: ["inline", "block"], |
| 118 | + insertion: ["append", "prepend"], //"before", "insertBefore", "after", "insertAfter" |
| 119 | + allErrors: [true, false], |
| 120 | + clear: ["focusin", "keypress", false], |
| 121 | + trigger: [ |
| 122 | + "click", "dblclick", "focusout", |
| 123 | + "hover", "mousedown", "mouseenter", |
| 124 | + "mouseleave", "mousemove", "mouseout", |
| 125 | + "mouseover", "mouseup", "toggle" |
| 126 | + ] |
| 127 | + } |
| 128 | + }, |
| 129 | + dynamic: { |
| 130 | + settings: { |
| 131 | + trigger: ["focusout", "keydown", "keypress", "keyup"] |
| 132 | + } |
| 133 | + }, |
| 134 | + debug: [true, false] |
| 135 | + }; |
136 | 136 |
|
137 | 137 | var Validation = function(node, options) { |
138 | 138 |
|
|
252 | 252 | options.debug && window.Debug.log({ |
253 | 253 | 'node': node, |
254 | 254 | 'function': 'delegateDynamicValidation()', |
255 | | - 'arguments': JSON.stringify(options), |
256 | 255 | 'message': 'OK - Dynamic Validation activated on ' + node.length + ' form(s)' |
257 | 256 | }); |
258 | 257 |
|
|
315 | 314 | options.debug && window.Debug.log({ |
316 | 315 | 'node': node, |
317 | 316 | 'function': 'delegateValidation()', |
318 | | - 'arguments': JSON.stringify(options), |
319 | 317 | 'message': 'OK - Validation activated on ' + node.length + ' form(s)' |
320 | 318 | }); |
321 | 319 |
|
|
368 | 366 |
|
369 | 367 | formData = {}; |
370 | 368 |
|
| 369 | + |
371 | 370 | $.each( |
372 | | - node.find('[' + _data.validation + ']:not([disabled]),[' + _data.regex + ']:not([disabled])'), |
| 371 | + node.find('input:not([type="submit"]), select, textarea'), |
373 | 372 | function(index, input) { |
374 | | - if (!validateInput(input)) { |
375 | | - isValid = false; |
| 373 | + |
| 374 | + input = $(input); |
| 375 | + |
| 376 | + var value = _getInputValue(input[0]), |
| 377 | + inputName = input.attr('name'); |
| 378 | + |
| 379 | + if (inputName) { |
| 380 | + formData[inputName] = value; |
| 381 | + } |
| 382 | + |
| 383 | + if (!input.attr('disabled') && (!!input.attr(_data.validation) || !!input.attr(_data.regex))) { |
| 384 | + if (!validateInput(input[0], value)) { |
| 385 | + isValid = false; |
| 386 | + } |
376 | 387 | } |
377 | 388 | } |
378 | 389 | ); |
|
422 | 433 |
|
423 | 434 | } |
424 | 435 |
|
425 | | - function validateInput(input) { |
| 436 | + function validateInput(input, value) { |
426 | 437 |
|
427 | | - var inputName = $(input).attr('name'); |
| 438 | + var inputName = $(input).attr('name'), |
| 439 | + value = value || _getInputValue(input); |
428 | 440 |
|
429 | 441 | if (!inputName) { |
430 | 442 | options.debug && window.Debug.log({ |
|
437 | 449 | return false; |
438 | 450 | } |
439 | 451 |
|
440 | | - var value = _getInputValue(input), |
441 | | - |
442 | | - matches = inputName.replace(/]$/, '').split(/]\[|[[\]]/g), |
| 452 | + var matches = inputName.replace(/]$/, '').split(/]\[|[[\]]/g), |
443 | 453 | inputShortName = window.Validation.labels[inputName] || |
444 | 454 | options.labels[inputName] || |
445 | 455 | $(input).attr(_data.label) || |
|
453 | 463 |
|
454 | 464 | validateOnce = false; |
455 | 465 |
|
456 | | - formData[inputName] = value; |
457 | | - |
458 | 466 | if (validationArray) { |
459 | 467 | validationArray = _api._splitValidation(validationArray); |
460 | 468 | } |
|
715 | 723 | } |
716 | 724 |
|
717 | 725 | for (var i = 0; i < errors[inputName].length; i++) { |
718 | | - errorContainer.find('ul').append('<li>' + errors[inputName][i] + '</li>'); |
| 726 | + errorContainer.find('[' + _data.errorList + '] ul').append('<li>' + errors[inputName][i] + '</li>'); |
719 | 727 | } |
720 | 728 |
|
721 | 729 | if (options.submit.settings.clear || options.dynamic.settings.trigger) { |
|
1056 | 1064 | if (!node[0] || node[0].nodeName.toLowerCase() !== "form") { |
1057 | 1065 | window.Debug.log({ |
1058 | 1066 | 'function': '$.validate()', |
1059 | | - 'arguments': JSON.stringify(options.submit.settings.form), |
| 1067 | + 'arguments': options.submit.settings.form, |
1060 | 1068 | 'message': 'Unable to find jQuery form element - Validation dropped' |
1061 | 1069 | }); |
1062 | 1070 |
|
|
1187 | 1195 | window.Debug.log({ |
1188 | 1196 | 'node': node, |
1189 | 1197 | 'function': '$.addError()', |
1190 | | - 'arguments': 'window.Validation.form[' + JSON.stringify(node.selector) + ']', |
| 1198 | + 'arguments': 'window.Validation.form[' + node.selector + ']', |
1191 | 1199 | 'message': 'ERROR - Invalid node selector' |
1192 | 1200 | }); |
1193 | 1201 |
|
|
1200 | 1208 | window.Debug.log({ |
1201 | 1209 | 'node': node, |
1202 | 1210 | 'function': '$.addError()', |
1203 | | - 'arguments': 'window.Validation.form[' + JSON.stringify(node.selector) + ']', |
| 1211 | + 'arguments': 'window.Validation.form[' + node.selector + ']', |
1204 | 1212 | 'message': 'ERROR - Invalid argument, must be type object' |
1205 | 1213 | }); |
1206 | 1214 |
|
|
1226 | 1234 | window.Debug.log({ |
1227 | 1235 | 'node': node, |
1228 | 1236 | 'function': '$.addError()', |
1229 | | - 'arguments': JSON.stringify(inputName), |
| 1237 | + 'arguments': inputName, |
1230 | 1238 | 'message': 'ERROR - Unable to find ' + '$(' + node.selector + ').find("[name="' + inputName + '"]")' |
1231 | 1239 | }); |
1232 | 1240 |
|
|
1248 | 1256 | window.Debug.log({ |
1249 | 1257 | 'node': node, |
1250 | 1258 | 'function': '$.addError()', |
1251 | | - 'arguments': JSON.stringify(error[inputName][i]), |
| 1259 | + 'arguments': error[inputName][i], |
1252 | 1260 | 'message': 'ERROR - Invalid error object property - Accepted format: {"inputName": "errorString"} or {"inputName": ["errorString", "errorString"]}' |
1253 | 1261 | }); |
1254 | 1262 |
|
|
1273 | 1281 | window.Debug.log({ |
1274 | 1282 | 'node': node, |
1275 | 1283 | 'function': '$.removeError()', |
1276 | | - 'arguments': 'window.Validation.form[' + JSON.stringify(node.selector) + ']', |
| 1284 | + 'arguments': 'window.Validation.form[' + node.selector + ']', |
1277 | 1285 | 'message': 'ERROR - Invalid node selector' |
1278 | 1286 | }); |
1279 | 1287 |
|
|
1291 | 1299 | window.Debug.log({ |
1292 | 1300 | 'node': node, |
1293 | 1301 | 'function': '$.removeError()', |
1294 | | - 'arguments': JSON.stringify(inputName), |
| 1302 | + 'arguments': inputName, |
1295 | 1303 | 'message': 'ERROR - Invalid inputName, must be type String or Array' |
1296 | 1304 | }); |
1297 | 1305 |
|
|
1312 | 1320 | window.Debug.log({ |
1313 | 1321 | 'node': node, |
1314 | 1322 | 'function': '$.removeError()', |
1315 | | - 'arguments': JSON.stringify(inputName[i]), |
| 1323 | + 'arguments': inputName[i], |
1316 | 1324 | 'message': 'ERROR - Unable to find ' + '$(' + node.selector + ').find("[name="' + inputName[i] + '"]")' |
1317 | 1325 | }); |
1318 | 1326 |
|
|
0 commit comments