|
4 | 4 | * Licensed under the MIT license |
5 | 5 | * |
6 | 6 | * @author Tom Bertrand |
7 | | - * @version 1.5.0 (2015-02-08) |
| 7 | + * @version 1.5.1 (2015-02-16) |
8 | 8 | * @link http://www.runningcoder.org/jqueryvalidation/ |
9 | 9 | * |
10 | 10 | * @note |
11 | 11 | * Remove debug code: //\s?\{debug\}[\s\S]*?\{/debug\} |
12 | 12 | */ |
13 | | -;(function (window, document, $, undefined) |
14 | | -{ |
| 13 | +;(function (window, document, $, undefined) { |
15 | 14 |
|
16 | 15 | window.Validation = { |
17 | 16 | form: [], |
|
23 | 22 | * Fail-safe preventExtensions function for older browsers |
24 | 23 | */ |
25 | 24 | if (typeof Object.preventExtensions !== "function") { |
26 | | - Object.preventExtensions = function (obj) { return obj; }; |
| 25 | + Object.preventExtensions = function (obj) { |
| 26 | + return obj; |
| 27 | + }; |
27 | 28 | } |
28 | 29 |
|
29 | 30 | // Not using strict to avoid throwing a window error on bad config extend. |
|
38 | 39 | var _rules = { |
39 | 40 | NOTEMPTY: /./, |
40 | 41 | INTEGER: /^\d+$/, |
41 | | - NUMERIC: /^\d+(?:[\s,]\d{3})*(?:\.\d+)?$/, |
| 42 | + NUMERIC: /^\d+(?:[,\s]\d{3})*(?:\.\d+)?$/, |
42 | 43 | MIXED: /^[\w\s-]+$/, |
43 | 44 | NOSPACE: /^(?!\s)\S*$/, |
44 | 45 | TRIM: /^[^\s].*[^\s]$/, |
45 | 46 | DATE: /^\d{4}-\d{2}-\d{2}(\s\d{2}:\d{2}(:\d{2})?)?$/, |
46 | 47 | EMAIL: /^([^@]+?)@(([a-z0-9]-*)*[a-z0-9]+\.)+([a-z0-9]+)$/i, |
47 | 48 | URL: /^(https?:\/\/)?((([a-z0-9]-*)*[a-z0-9]+\.?)*([a-z0-9]+))(\/[\w?=\.-]*)*$/, |
48 | 49 | PHONE: /^(\()?\d{3}(\))?(-|\s)?\d{3}(-|\s)\d{4}$/, |
49 | | - OPTIONAL: /^.*$/, |
| 50 | + OPTIONAL: /./, |
50 | 51 | COMPARISON: /^\s*([LV])\s*([<>]=?|==|!=)\s*([^<>=!]+?)\s*$/ |
51 | 52 | }, |
52 | 53 |
|
|
185 | 186 | /** |
186 | 187 | * Extends user-defined "options.message" into the default Validation "_message". |
187 | 188 | */ |
188 | | - function extendRules () { |
| 189 | + function extendRules() { |
189 | 190 | options.rules = $.extend( |
190 | 191 | true, |
191 | 192 | {}, |
|
197 | 198 | /** |
198 | 199 | * Extends user-defined "options.message" into the default Validation "_message". |
199 | 200 | */ |
200 | | - function extendMessages () { |
| 201 | + function extendMessages() { |
201 | 202 | options.messages = $.extend( |
202 | 203 | true, |
203 | 204 | {}, |
|
212 | 213 | * - preventExtensions prevents from modifying the Validation "_options" object structure |
213 | 214 | * - filter through the "_supported" to delete unsupported "options" |
214 | 215 | */ |
215 | | - function extendOptions () { |
| 216 | + function extendOptions() { |
216 | 217 |
|
217 | 218 | if (!(options instanceof Object)) { |
218 | 219 | options = {}; |
|
330 | 331 | }); |
331 | 332 | // {/debug} |
332 | 333 |
|
333 | | - if ( !node.find('[' + _data.validation + '],[' + _data.regex + ']')[0]) { |
| 334 | + if (!node.find('[' + _data.validation + '],[' + _data.regex + ']')[0]) { |
334 | 335 |
|
335 | 336 | // {debug} |
336 | 337 | options.debug && window.Debug.log({ |
|
390 | 391 | * Delegates the submit validation on data-validation and data-validation-regex attributes based on trigger. |
391 | 392 | * Note: Disable the form submit function so the callbacks are not by-passed |
392 | 393 | */ |
393 | | - function delegateValidation () { |
| 394 | + function delegateValidation() { |
394 | 395 |
|
395 | 396 | _executeCallback(options.submit.callback.onInit, [node]); |
396 | 397 |
|
|
462 | 463 | * |
463 | 464 | * @returns {boolean} true if no error(s) were found (valid form) |
464 | 465 | */ |
465 | | - function validateForm () { |
| 466 | + function validateForm() { |
466 | 467 |
|
467 | 468 | var isValid = true; |
468 | 469 |
|
469 | 470 | $.each( |
470 | | - node.find('[' + _data.validation + ']:not([disabled]),[' + _data.regex + ']:not([disabled])'), |
| 471 | + node.find('[' + _data.validation + ']:not([disabled],[readonly]),[' + _data.regex + ']:not([disabled],[readonly])'), |
471 | 472 | function (index, input) { |
472 | 473 | if (!validateInput(input)) { |
473 | 474 | isValid = false; |
|
487 | 488 | * |
488 | 489 | * @returns {boolean} true if no error(s) were found (valid input) |
489 | 490 | */ |
490 | | - function validateInput (input) { |
| 491 | + function validateInput(input) { |
491 | 492 |
|
492 | 493 | var inputName = $(input).attr('name'); |
493 | 494 |
|
|
529 | 530 | if (validationArray instanceof Array && validationArray.length > 0) { |
530 | 531 |
|
531 | 532 | // "OPTIONAL" input will not be validated if it's empty |
532 | | - if (value === '' && $.inArray('OPTIONAL', validationArray) !== -1) { |
| 533 | + if (value === '' && ~validationArray.indexOf('OPTIONAL')) { |
533 | 534 | return true; |
534 | 535 | } |
535 | 536 |
|
|
597 | 598 | * |
598 | 599 | * @returns {*} Error if a mismatch occurred. |
599 | 600 | */ |
600 | | - function validateRule (value, rule, reversed) { |
| 601 | + function validateRule(value, rule, reversed) { |
601 | 602 |
|
602 | 603 | // Validate for "data-validation-regex" and "data-validation-regex-reverse" |
603 | 604 | if (rule instanceof RegExp) { |
|
716 | 717 | * @param {string} inputName Input where the error occurred |
717 | 718 | * @param {string} error Description of the error to be displayed |
718 | 719 | */ |
719 | | - function registerError (inputName, error) { |
| 720 | + function registerError(inputName, error) { |
720 | 721 |
|
721 | 722 | if (!errors[inputName]) { |
722 | 723 | errors[inputName] = []; |
|
747 | 748 | * |
748 | 749 | * @returns {boolean} false if an unwanted behavior occurs |
749 | 750 | */ |
750 | | - function displayOneError (inputName) { |
| 751 | + function displayOneError(inputName) { |
751 | 752 |
|
752 | 753 | var input, |
753 | 754 | inputId, |
|
859 | 860 | } |
860 | 861 | } |
861 | 862 |
|
862 | | - input.unbind(event).on(event, function (a,b,c,d,e) { |
| 863 | + input.unbind(event).on(event, function (a, b, c, d, e) { |
863 | 864 |
|
864 | 865 | return function () { |
865 | 866 | if (e) { |
866 | 867 | if ($(c).hasClass(options.submit.settings.errorClass)) { |
867 | | - resetOneError(a,b,c,d,e); |
| 868 | + resetOneError(a, b, c, d, e); |
868 | 869 | } |
869 | 870 | } else if ($(b).hasClass(options.submit.settings.errorClass)) { |
870 | | - resetOneError(a,b,c,d); |
| 871 | + resetOneError(a, b, c, d); |
871 | 872 | } |
872 | 873 | }; |
873 | 874 |
|
|
893 | 894 | /** |
894 | 895 | * Display all of the errors |
895 | 896 | */ |
896 | | - function displayErrors () { |
| 897 | + function displayErrors() { |
897 | 898 |
|
898 | 899 | for (var inputName in errors) { |
899 | 900 | if (!errors.hasOwnProperty(inputName)) continue; |
|
959 | 960 | /** |
960 | 961 | * Remove all of the input error(s) display. |
961 | 962 | */ |
962 | | - function resetErrors () { |
| 963 | + function resetErrors() { |
963 | 964 |
|
964 | 965 | errors = []; |
965 | 966 | window.Validation.hasScrolled = false; |
|
975 | 976 | * - This function will be overridden if "options.submit.settings.onSubmit" is defined |
976 | 977 | * - The node can't be submitted by jQuery since it has been disabled, use the form native submit function instead |
977 | 978 | */ |
978 | | - function submitForm () { |
| 979 | + function submitForm() { |
979 | 980 |
|
980 | 981 | node[0].submit() |
981 | 982 |
|
|
986 | 987 | * |
987 | 988 | * @returns {boolean} |
988 | 989 | */ |
989 | | - function destroy () { |
| 990 | + function destroy() { |
990 | 991 |
|
991 | 992 | resetErrors(); |
992 | 993 | node.find('[' + _data.validation + '],[' + _data.regex + ']').off(delegateSuffix + ' ' + resetSuffix); |
|
1035 | 1036 | * Execute function once the timer is reached. |
1036 | 1037 | * If the function is recalled before the timer ends, the first call will be canceled. |
1037 | 1038 | */ |
1038 | | - var _typeWatch = (function(){ |
| 1039 | + var _typeWatch = (function () { |
1039 | 1040 | var timer = 0; |
1040 | | - return function(callback, ms){ |
1041 | | - clearTimeout (timer); |
| 1041 | + return function (callback, ms) { |
| 1042 | + clearTimeout(timer); |
1042 | 1043 | timer = setTimeout(callback, ms); |
1043 | 1044 | }; |
1044 | 1045 | })(); |
|
1288 | 1289 | rules = [rules]; |
1289 | 1290 | } |
1290 | 1291 |
|
1291 | | - for (var i=0; i<rules.length; i++) { |
| 1292 | + for (var i = 0; i < rules.length; i++) { |
1292 | 1293 | _api.alterValidationRules(rules[i]); |
1293 | 1294 | } |
1294 | 1295 |
|
|
1480 | 1481 | return false; |
1481 | 1482 | } |
1482 | 1483 |
|
1483 | | - return node.each( function () { |
| 1484 | + return node.each(function () { |
1484 | 1485 |
|
1485 | 1486 | var $this = $(this), |
1486 | 1487 | validationData = $this.attr(_data.validation), |
|
1521 | 1522 | return false; |
1522 | 1523 | } |
1523 | 1524 |
|
1524 | | - return node.each( function () { |
| 1525 | + return node.each(function () { |
1525 | 1526 |
|
1526 | 1527 | var $this = $(this), |
1527 | 1528 | validationData = $this.attr(_data.validation), |
|
1618 | 1619 | error[inputName] = [error[inputName]]; |
1619 | 1620 | } |
1620 | 1621 |
|
1621 | | - input = $(node.selector).find('[name="'+ inputName + '"]'); |
| 1622 | + input = $(node.selector).find('[name="' + inputName + '"]'); |
1622 | 1623 | if (!input[0]) { |
1623 | 1624 |
|
1624 | 1625 | // {debug} |
1625 | 1626 | window.Debug.log({ |
1626 | 1627 | 'node': node, |
1627 | 1628 | 'function': '$.addError()', |
1628 | 1629 | 'arguments': JSON.stringify(inputName), |
1629 | | - 'message': 'ERROR - Unable to find ' + '$(' + node.selector + ').find("[name="'+ inputName + '"]")' |
| 1630 | + 'message': 'ERROR - Unable to find ' + '$(' + node.selector + ').find("[name="' + inputName + '"]")' |
1630 | 1631 | }); |
1631 | 1632 |
|
1632 | 1633 | window.Debug.print(); |
|
1731 | 1732 | var input; |
1732 | 1733 | for (var i = 0; i < inputName.length; i++) { |
1733 | 1734 |
|
1734 | | - input = $(node.selector).find('[name="'+ inputName[i] + '"]'); |
| 1735 | + input = $(node.selector).find('[name="' + inputName[i] + '"]'); |
1735 | 1736 | if (!input[0]) { |
1736 | 1737 |
|
1737 | 1738 | // {debug} |
1738 | 1739 | window.Debug.log({ |
1739 | 1740 | 'node': node, |
1740 | 1741 | 'function': '$.removeError()', |
1741 | 1742 | 'arguments': JSON.stringify(inputName[i]), |
1742 | | - 'message': 'ERROR - Unable to find ' + '$(' + node.selector + ').find("[name="'+ inputName[i] + '"]")' |
| 1743 | + 'message': 'ERROR - Unable to find ' + '$(' + node.selector + ').find("[name="' + inputName[i] + '"]")' |
1743 | 1744 | }); |
1744 | 1745 |
|
1745 | 1746 | window.Debug.print(); |
|
1912 | 1913 | console.table(this.table); |
1913 | 1914 | } else { |
1914 | 1915 | $.each(this.table, function (index, data) { |
1915 | | - console.log(data['Name'] + ': ' + data['Execution Time']+'ms'); |
| 1916 | + console.log(data['Name'] + ': ' + data['Execution Time'] + 'ms'); |
1916 | 1917 | }); |
1917 | 1918 | } |
1918 | 1919 |
|
|
1929 | 1930 | }; |
1930 | 1931 | // {/debug} |
1931 | 1932 |
|
1932 | | - String.prototype.capitalize = function() { |
| 1933 | + String.prototype.capitalize = function () { |
1933 | 1934 | return this.charAt(0).toUpperCase() + this.slice(1); |
1934 | 1935 | }; |
1935 | 1936 |
|
1936 | | - if (!Array.prototype.indexOf) |
1937 | | - { |
1938 | | - Array.prototype.indexOf = function(elt /*, from*/) |
1939 | | - { |
| 1937 | + if (!Array.prototype.indexOf) { |
| 1938 | + Array.prototype.indexOf = function (elt /*, from*/) { |
1940 | 1939 | var len = this.length >>> 0; |
1941 | 1940 |
|
1942 | 1941 | var from = Number(arguments[1]) || 0; |
|
1946 | 1945 | if (from < 0) |
1947 | 1946 | from += len; |
1948 | 1947 |
|
1949 | | - for (; from < len; from++) |
1950 | | - { |
| 1948 | + for (; from < len; from++) { |
1951 | 1949 | if (from in this && |
1952 | 1950 | this[from] === elt) |
1953 | 1951 | return from; |
|
0 commit comments