|
2 | 2 | * jQuery Form Validation |
3 | 3 | * |
4 | 4 | * @author Tom Bertrand |
5 | | - * @version 1.3.5 (2014-09-10) |
| 5 | + * @version 1.4.0 (2014-09-28) |
6 | 6 | * |
7 | 7 | * @copyright |
8 | 8 | * Copyright (C) 2014 RunningCoder. |
|
72 | 72 | * Error messages |
73 | 73 | */ |
74 | 74 | var _messages = Object.preventExtensions({ |
75 | | - 'default': '$ contain error(s).', |
76 | | - 'NOTEMPTY': '$ must not be empty.', |
77 | | - 'NUMERIC': '$ must be numeric.', |
78 | | - 'STRING': '$ must be a string.', |
79 | | - 'NOSPACE': '$ must not contain spaces.', |
80 | | - 'TRIM': '$ must not start or end with space character.', |
81 | | - 'MIXED': '$ must be letters or numbers (no special characters).', |
82 | | - 'DATE': '$ is not a valid with format YYYY-MM-DD.', |
83 | | - 'EMAIL': '$ is not valid.', |
84 | | - 'URL': '$ is not valid.', |
85 | | - 'PHONE': '$ is not a valid phone number.', |
86 | | - //'INARRAY': '$ is not a valid option.', |
87 | | - '<': '$ must be less than % characters.', |
88 | | - '<=': '$ must be less or equal to % characters.', |
89 | | - '>': '$ must be greater than % characters.', |
90 | | - '>=': '$ must be greater or equal to % characters.', |
91 | | - '==': '$ must be equal to %', |
92 | | - '!=': '$ must be different than %' |
93 | | - }), |
| 75 | + 'default': '$ contain error(s).', |
| 76 | + 'NOTEMPTY': '$ must not be empty.', |
| 77 | + 'NUMERIC': '$ must be numeric.', |
| 78 | + 'STRING': '$ must be a string.', |
| 79 | + 'NOSPACE': '$ must not contain spaces.', |
| 80 | + 'TRIM': '$ must not start or end with space character.', |
| 81 | + 'MIXED': '$ must be letters or numbers (no special characters).', |
| 82 | + 'DATE': '$ is not a valid with format YYYY-MM-DD.', |
| 83 | + 'EMAIL': '$ is not valid.', |
| 84 | + 'URL': '$ is not valid.', |
| 85 | + 'PHONE': '$ is not a valid phone number.', |
| 86 | + //'INARRAY': '$ is not a valid option.', |
| 87 | + '<': '$ must be less than % characters.', |
| 88 | + '<=': '$ must be less or equal to % characters.', |
| 89 | + '>': '$ must be greater than % characters.', |
| 90 | + '>=': '$ must be greater or equal to % characters.', |
| 91 | + '==': '$ must be equal to %', |
| 92 | + '!=': '$ must be different than %' |
| 93 | + }), |
94 | 94 | _extendedMessages = false; |
95 | 95 |
|
96 | 96 | /** |
|
113 | 113 | * |
114 | 114 | * @link http://www.runningcoder.org/jqueryvalidation/documentation/ |
115 | 115 | */ |
116 | | - var _options = { |
117 | | - submit: { |
118 | | - settings: { |
119 | | - form: null, |
120 | | - display: "inline", |
121 | | - insertion: "append", |
122 | | - allErrors: false, |
123 | | - trigger: "click", |
124 | | - button: "input[type='submit']", |
125 | | - errorClass: "error", |
126 | | - errorListClass: "error-list", |
127 | | - inputContainer: null, |
128 | | - clear: "focusin", |
129 | | - scrollToError: false |
130 | | - }, |
131 | | - callback: { |
132 | | - onInit: null, |
133 | | - onValidate: null, |
134 | | - onError: null, |
135 | | - onBeforeSubmit: null, |
136 | | - onSubmit: null, |
137 | | - onAfterSubmit: null |
138 | | - } |
139 | | - }, |
140 | | - dynamic: { |
141 | | - settings: { |
142 | | - trigger: null, |
143 | | - delay: 300 |
144 | | - }, |
145 | | - callback: { |
146 | | - onSuccess: null, |
147 | | - onError: null, |
148 | | - onComplete: null |
149 | | - } |
150 | | - }, |
151 | | - messages: {}, |
152 | | - labels: {}, |
153 | | - debug: false |
154 | | - }; |
| 116 | + var _options = { |
| 117 | + submit: { |
| 118 | + settings: { |
| 119 | + form: null, |
| 120 | + display: "inline", |
| 121 | + insertion: "append", |
| 122 | + allErrors: false, |
| 123 | + trigger: "click", |
| 124 | + button: "input[type='submit']", |
| 125 | + errorClass: "error", |
| 126 | + errorListClass: "error-list", |
| 127 | + inputContainer: null, |
| 128 | + clear: "focusin", |
| 129 | + scrollToError: false |
| 130 | + }, |
| 131 | + callback: { |
| 132 | + onInit: null, |
| 133 | + onValidate: null, |
| 134 | + onError: null, |
| 135 | + onBeforeSubmit: null, |
| 136 | + onSubmit: null, |
| 137 | + onAfterSubmit: null |
| 138 | + } |
| 139 | + }, |
| 140 | + dynamic: { |
| 141 | + settings: { |
| 142 | + trigger: null, |
| 143 | + delay: 300 |
| 144 | + }, |
| 145 | + callback: { |
| 146 | + onSuccess: null, |
| 147 | + onError: null, |
| 148 | + onComplete: null |
| 149 | + } |
| 150 | + }, |
| 151 | + messages: {}, |
| 152 | + labels: {}, |
| 153 | + debug: false |
| 154 | + }; |
155 | 155 |
|
156 | 156 | /** |
157 | 157 | * @private |
|
191 | 191 | */ |
192 | 192 | var Validation = function (node, options) { |
193 | 193 |
|
194 | | - var errors = []; |
195 | | - window.Validation.hasScrolled = false; |
| 194 | + var errors = [], |
| 195 | + delegateSuffix = ".vd", // validation.delegate |
| 196 | + resetSuffix = ".vr"; // validation.resetError |
| 197 | + |
| 198 | + window.Validation.hasScrolled = false; |
196 | 199 |
|
197 | 200 | /** |
198 | 201 | * Extends user-defined "message" into the default Validation "_message". |
|
352 | 355 | return false; |
353 | 356 | } |
354 | 357 |
|
355 | | - var namespace = ".vd", // validation.delegate |
356 | | - event = options.dynamic.settings.trigger + namespace; |
| 358 | + var event = options.dynamic.settings.trigger + delegateSuffix; |
357 | 359 | if (options.dynamic.settings.trigger !== "focusout") { |
358 | | - event += " change" + namespace + " paste" + namespace; |
| 360 | + event += " change" + delegateSuffix + " paste" + delegateSuffix; |
359 | 361 | } |
360 | 362 |
|
361 | 363 | $.each( |
|
419 | 421 | // {debug} |
420 | 422 | options.debug && window.Debug.log({ |
421 | 423 | 'node': node, |
422 | | - 'function': 'delegateDynamicValidation()', |
423 | | - 'arguments': 'node.find(' + options.submit.settings.button + ')', |
424 | | - 'message': 'ERROR - ' + options.submit.settings.button + ' not found' |
| 424 | + 'function': 'delegateValidation()', |
| 425 | + 'arguments': '{button: ' + options.submit.settings.button + '}', |
| 426 | + 'message': 'ERROR - node.find("' + options.submit.settings.button + '") not found' |
425 | 427 | }); |
426 | 428 | // {/debug} |
427 | 429 |
|
|
524 | 526 |
|
525 | 527 | matches = inputName.replace(/]$/, '').split(/]\[|[[\]]/g), |
526 | 528 | inputShortName = window.Validation.labels[inputName] || |
527 | | - options.labels[inputName] || |
528 | | - $(input).attr(_data.label) || |
529 | | - matches[matches.length - 1], |
| 529 | + options.labels[inputName] || |
| 530 | + $(input).attr(_data.label) || |
| 531 | + matches[matches.length - 1], |
530 | 532 |
|
531 | 533 | validationArray = $(input).attr(_data.validation), |
532 | 534 | validationMessage = $(input).attr(_data.validationMessage), |
|
744 | 746 | throw [options.messages[operator].replace(' characters', ''), compared]; |
745 | 747 | } |
746 | 748 |
|
747 | | - // Compare numeric value |
748 | 749 | } else { |
749 | | - |
| 750 | + // Compare numeric value |
750 | 751 | if (!value || isNaN(value) || !eval(value + operator + parseFloat(compared))) { |
751 | 752 | throw [options.messages[operator].replace(' characters', ''), compared]; |
752 | 753 | } |
|
878 | 879 |
|
879 | 880 | if (options.submit.settings.display === "inline" || |
880 | 881 | (options.submit.settings.display === "block" && !errorContainer.find('[' + _data.errorList + ']')[0]) |
881 | | - ) { |
| 882 | + ) { |
882 | 883 | if (options.submit.settings.insertion === 'append') { |
883 | 884 | errorContainer.append(html); |
884 | 885 | } else if (options.submit.settings.insertion === 'prepend') { |
|
896 | 897 | input = groupInput; |
897 | 898 | } |
898 | 899 |
|
899 | | - var namespace = ".vr", //validation.resetError |
900 | | - event = "coucou" + namespace; |
| 900 | + var event = "coucou" + resetSuffix; |
901 | 901 | if (options.submit.settings.clear) { |
902 | | - event += " " + options.submit.settings.clear + namespace |
| 902 | + event += " " + options.submit.settings.clear + resetSuffix |
903 | 903 | } |
904 | 904 | if (options.dynamic.settings.trigger) { |
905 | | - event += " " + options.dynamic.settings.trigger + namespace; |
| 905 | + event += " " + options.dynamic.settings.trigger + resetSuffix; |
906 | 906 | if (options.dynamic.settings.trigger !== "focusout") { |
907 | | - event += " change" + namespace + " paste" + namespace; |
| 907 | + event += " change" + resetSuffix + " paste" + resetSuffix; |
908 | 908 | } |
909 | 909 | } |
910 | 910 |
|
|
999 | 999 | } |
1000 | 1000 | } |
1001 | 1001 |
|
1002 | | - //$._data( input[0], "events" ); |
1003 | | - input.trigger('coucou.vr'); |
1004 | | - |
| 1002 | + input.trigger('coucou' + resetSuffix); |
1005 | 1003 | } |
1006 | 1004 |
|
1007 | 1005 | } |
|
1031 | 1029 |
|
1032 | 1030 | } |
1033 | 1031 |
|
| 1032 | + /** |
| 1033 | + * Submits the form once it succeeded the validation process. |
| 1034 | + * Note: |
| 1035 | + * - This function will be overridden if "options.submit.settings.onSubmit" is defined |
| 1036 | + * - The node can't be submitted by jQuery since it has been disabled, use the form native submit function instead |
| 1037 | + * |
| 1038 | + * @returns {boolean} |
| 1039 | + */ |
| 1040 | + function destroy () { |
| 1041 | + |
| 1042 | + var event = options.submit.settings.trigger + delegateSuffix; |
| 1043 | + |
| 1044 | + if (options.dynamic.settings.trigger) { |
| 1045 | + event += " " + options.dynamic.settings.trigger + delegateSuffix; |
| 1046 | + if (options.dynamic.settings.trigger !== "focusout") { |
| 1047 | + event += " change" + delegateSuffix + " paste" + delegateSuffix; |
| 1048 | + } |
| 1049 | + } |
| 1050 | + |
| 1051 | + $.each( |
| 1052 | + node.find('[' + _data.validation + '],[' + _data.regex + ']'), |
| 1053 | + function (i, v) { |
| 1054 | + $(v).unbind(event); |
| 1055 | + } |
| 1056 | + ); |
| 1057 | + |
| 1058 | + //delete window.Validation.form[node.selector]; |
| 1059 | + |
| 1060 | + return true; |
| 1061 | + |
| 1062 | + } |
| 1063 | + |
1034 | 1064 | /** |
1035 | 1065 | * @private |
1036 | 1066 | * Helper to get the value of an regular, radio or chackbox input |
|
1206 | 1236 | * @public |
1207 | 1237 | * Remove all errors |
1208 | 1238 | */ |
1209 | | - resetErrors: resetErrors |
| 1239 | + resetErrors: resetErrors, |
| 1240 | + |
| 1241 | + /** |
| 1242 | + * @public |
| 1243 | + * Destroy the Validation instance |
| 1244 | + */ |
| 1245 | + destroy: destroy |
1210 | 1246 |
|
1211 | 1247 | }; |
1212 | 1248 |
|
|
1429 | 1465 | return; |
1430 | 1466 | } |
1431 | 1467 |
|
| 1468 | + if (options === "destroy") { |
| 1469 | + |
| 1470 | + if (!window.Validation.form[node.selector]) { |
| 1471 | + |
| 1472 | + // {debug} |
| 1473 | + window.Debug.log({ |
| 1474 | + 'node': node, |
| 1475 | + 'function': '$.validate("destroy")', |
| 1476 | + 'arguments': '', |
| 1477 | + 'message': 'Unable to destroy "' + node.selector + '", perhaps it\'s already destroyed?' |
| 1478 | + }); |
| 1479 | + |
| 1480 | + window.Debug.print(); |
| 1481 | + // {/debug} |
| 1482 | + |
| 1483 | + return; |
| 1484 | + } |
| 1485 | + |
| 1486 | + window.Validation.form[node.selector].destroy(); |
| 1487 | + |
| 1488 | + return; |
| 1489 | + |
| 1490 | + } |
| 1491 | + |
1432 | 1492 | return node.each(function () { |
1433 | 1493 | window.Validation.form[node.selector] = new Validation($(this), options); |
1434 | 1494 | }); |
|
0 commit comments