|
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: /\S/, |
40 | 41 | INTEGER: /^\d+$/, |
41 | | - NUMERIC: /^\d+(?:[,|\s]\d{3})?(?:\.\d+)?$/, |
42 | | - MIXED: /^['\w\s-]+$/, |
| 42 | + NUMERIC: /^\d+(?:[,\s]\d{3})*(?:\.\d+)?$/, |
| 43 | + MIXED: /^[\w\s-]+$/, |
| 44 | + NAME: /^['a-z\s-]+$/i, |
43 | 45 | NOSPACE: /^(?!\s)\S*$/, |
44 | 46 | TRIM: /^[^\s].*[^\s]$/, |
45 | 47 | DATE: /^\d{4}-\d{2}-\d{2}(\s\d{2}:\d{2}(:\d{2})?)?$/, |
|
57 | 59 | _messages = { |
58 | 60 | 'default': '$ contain error(s).', |
59 | 61 | 'NOTEMPTY': '$ must not be empty.', |
60 | | - 'NUMERIC': '$ must be numeric.', |
61 | 62 | 'INTEGER': '$ must be an integer.', |
62 | | - 'STRING': '$ must be a string.', |
| 63 | + 'NUMERIC': '$ must be numeric.', |
| 64 | + 'MIXED': '$ must be letters or numbers (no special characters).', |
| 65 | + 'NAME': '$ must not contain special characters.', |
63 | 66 | 'NOSPACE': '$ must not contain spaces.', |
64 | 67 | 'TRIM': '$ must not start or end with space character.', |
65 | | - 'MIXED': '$ must be letters or numbers (no special characters).', |
66 | 68 | 'DATE': '$ is not a valid with format YYYY-MM-DD.', |
67 | 69 | 'EMAIL': '$ is not valid.', |
68 | 70 | 'URL': '$ is not valid.', |
|
185 | 187 | /** |
186 | 188 | * Extends user-defined "options.message" into the default Validation "_message". |
187 | 189 | */ |
188 | | - function extendRules () { |
| 190 | + function extendRules() { |
189 | 191 | options.rules = $.extend( |
190 | 192 | true, |
191 | 193 | {}, |
|
197 | 199 | /** |
198 | 200 | * Extends user-defined "options.message" into the default Validation "_message". |
199 | 201 | */ |
200 | | - function extendMessages () { |
| 202 | + function extendMessages() { |
201 | 203 | options.messages = $.extend( |
202 | 204 | true, |
203 | 205 | {}, |
|
212 | 214 | * - preventExtensions prevents from modifying the Validation "_options" object structure |
213 | 215 | * - filter through the "_supported" to delete unsupported "options" |
214 | 216 | */ |
215 | | - function extendOptions () { |
| 217 | + function extendOptions() { |
216 | 218 |
|
217 | 219 | if (!(options instanceof Object)) { |
218 | 220 | options = {}; |
|
330 | 332 | }); |
331 | 333 | // {/debug} |
332 | 334 |
|
333 | | - if ( !node.find('[' + _data.validation + '],[' + _data.regex + ']')[0]) { |
| 335 | + if (!node.find('[' + _data.validation + '],[' + _data.regex + ']')[0]) { |
334 | 336 |
|
335 | 337 | // {debug} |
336 | 338 | options.debug && window.Debug.log({ |
|
390 | 392 | * Delegates the submit validation on data-validation and data-validation-regex attributes based on trigger. |
391 | 393 | * Note: Disable the form submit function so the callbacks are not by-passed |
392 | 394 | */ |
393 | | - function delegateValidation () { |
| 395 | + function delegateValidation() { |
394 | 396 |
|
395 | 397 | _executeCallback(options.submit.callback.onInit, [node]); |
396 | 398 |
|
|
462 | 464 | * |
463 | 465 | * @returns {boolean} true if no error(s) were found (valid form) |
464 | 466 | */ |
465 | | - function validateForm () { |
| 467 | + function validateForm() { |
466 | 468 |
|
467 | 469 | var isValid = true; |
468 | 470 |
|
469 | 471 | $.each( |
470 | | - node.find('[' + _data.validation + ']:not([disabled]),[' + _data.regex + ']:not([disabled])'), |
| 472 | + node.find('[' + _data.validation + ']:not([disabled],[readonly]),[' + _data.regex + ']:not([disabled],[readonly])'), |
471 | 473 | function (index, input) { |
472 | 474 | if (!validateInput(input)) { |
473 | 475 | isValid = false; |
|
487 | 489 | * |
488 | 490 | * @returns {boolean} true if no error(s) were found (valid input) |
489 | 491 | */ |
490 | | - function validateInput (input) { |
| 492 | + function validateInput(input) { |
491 | 493 |
|
492 | 494 | var inputName = $(input).attr('name'); |
493 | 495 |
|
|
529 | 531 | if (validationArray instanceof Array && validationArray.length > 0) { |
530 | 532 |
|
531 | 533 | // "OPTIONAL" input will not be validated if it's empty |
532 | | - if (value === '' && $.inArray('OPTIONAL', validationArray) !== -1) { |
| 534 | + if (value === '' && ~validationArray.indexOf('OPTIONAL')) { |
533 | 535 | return true; |
534 | 536 | } |
535 | 537 |
|
|
597 | 599 | * |
598 | 600 | * @returns {*} Error if a mismatch occurred. |
599 | 601 | */ |
600 | | - function validateRule (value, rule, reversed) { |
| 602 | + function validateRule(value, rule, reversed) { |
601 | 603 |
|
602 | 604 | // Validate for "data-validation-regex" and "data-validation-regex-reverse" |
603 | 605 | if (rule instanceof RegExp) { |
|
716 | 718 | * @param {string} inputName Input where the error occurred |
717 | 719 | * @param {string} error Description of the error to be displayed |
718 | 720 | */ |
719 | | - function registerError (inputName, error) { |
| 721 | + function registerError(inputName, error) { |
720 | 722 |
|
721 | 723 | if (!errors[inputName]) { |
722 | 724 | errors[inputName] = []; |
|
747 | 749 | * |
748 | 750 | * @returns {boolean} false if an unwanted behavior occurs |
749 | 751 | */ |
750 | | - function displayOneError (inputName) { |
| 752 | + function displayOneError(inputName) { |
751 | 753 |
|
752 | 754 | var input, |
753 | 755 | inputId, |
|
859 | 861 | } |
860 | 862 | } |
861 | 863 |
|
862 | | - input.unbind(event).on(event, function (a,b,c,d,e) { |
| 864 | + input.unbind(event).on(event, function (a, b, c, d, e) { |
863 | 865 |
|
864 | 866 | return function () { |
865 | 867 | if (e) { |
866 | 868 | if ($(c).hasClass(options.submit.settings.errorClass)) { |
867 | | - resetOneError(a,b,c,d,e); |
| 869 | + resetOneError(a, b, c, d, e); |
868 | 870 | } |
869 | 871 | } else if ($(b).hasClass(options.submit.settings.errorClass)) { |
870 | | - resetOneError(a,b,c,d); |
| 872 | + resetOneError(a, b, c, d); |
871 | 873 | } |
872 | 874 | }; |
873 | 875 |
|
|
893 | 895 | /** |
894 | 896 | * Display all of the errors |
895 | 897 | */ |
896 | | - function displayErrors () { |
| 898 | + function displayErrors() { |
897 | 899 |
|
898 | 900 | for (var inputName in errors) { |
899 | 901 | if (!errors.hasOwnProperty(inputName)) continue; |
|
959 | 961 | /** |
960 | 962 | * Remove all of the input error(s) display. |
961 | 963 | */ |
962 | | - function resetErrors () { |
| 964 | + function resetErrors() { |
963 | 965 |
|
964 | 966 | errors = []; |
965 | 967 | window.Validation.hasScrolled = false; |
|
975 | 977 | * - This function will be overridden if "options.submit.settings.onSubmit" is defined |
976 | 978 | * - The node can't be submitted by jQuery since it has been disabled, use the form native submit function instead |
977 | 979 | */ |
978 | | - function submitForm () { |
| 980 | + function submitForm() { |
979 | 981 |
|
980 | 982 | node[0].submit() |
981 | 983 |
|
|
986 | 988 | * |
987 | 989 | * @returns {boolean} |
988 | 990 | */ |
989 | | - function destroy () { |
| 991 | + function destroy() { |
990 | 992 |
|
991 | 993 | resetErrors(); |
992 | 994 | node.find('[' + _data.validation + '],[' + _data.regex + ']').off(delegateSuffix + ' ' + resetSuffix); |
|
1035 | 1037 | * Execute function once the timer is reached. |
1036 | 1038 | * If the function is recalled before the timer ends, the first call will be canceled. |
1037 | 1039 | */ |
1038 | | - var _typeWatch = (function(){ |
| 1040 | + var _typeWatch = (function () { |
1039 | 1041 | var timer = 0; |
1040 | | - return function(callback, ms){ |
1041 | | - clearTimeout (timer); |
| 1042 | + return function (callback, ms) { |
| 1043 | + clearTimeout(timer); |
1042 | 1044 | timer = setTimeout(callback, ms); |
1043 | 1045 | }; |
1044 | 1046 | })(); |
|
1288 | 1290 | rules = [rules]; |
1289 | 1291 | } |
1290 | 1292 |
|
1291 | | - for (var i=0; i<rules.length; i++) { |
| 1293 | + for (var i = 0; i < rules.length; i++) { |
1292 | 1294 | _api.alterValidationRules(rules[i]); |
1293 | 1295 | } |
1294 | 1296 |
|
|
1480 | 1482 | return false; |
1481 | 1483 | } |
1482 | 1484 |
|
1483 | | - return node.each( function () { |
| 1485 | + return node.each(function () { |
1484 | 1486 |
|
1485 | 1487 | var $this = $(this), |
1486 | 1488 | validationData = $this.attr(_data.validation), |
|
1521 | 1523 | return false; |
1522 | 1524 | } |
1523 | 1525 |
|
1524 | | - return node.each( function () { |
| 1526 | + return node.each(function () { |
1525 | 1527 |
|
1526 | 1528 | var $this = $(this), |
1527 | 1529 | validationData = $this.attr(_data.validation), |
|
1618 | 1620 | error[inputName] = [error[inputName]]; |
1619 | 1621 | } |
1620 | 1622 |
|
1621 | | - input = $(node.selector).find('[name="'+ inputName + '"]'); |
| 1623 | + input = $(node.selector).find('[name="' + inputName + '"]'); |
1622 | 1624 | if (!input[0]) { |
1623 | 1625 |
|
1624 | 1626 | // {debug} |
1625 | 1627 | window.Debug.log({ |
1626 | 1628 | 'node': node, |
1627 | 1629 | 'function': '$.addError()', |
1628 | 1630 | 'arguments': JSON.stringify(inputName), |
1629 | | - 'message': 'ERROR - Unable to find ' + '$(' + node.selector + ').find("[name="'+ inputName + '"]")' |
| 1631 | + 'message': 'ERROR - Unable to find ' + '$(' + node.selector + ').find("[name="' + inputName + '"]")' |
1630 | 1632 | }); |
1631 | 1633 |
|
1632 | 1634 | window.Debug.print(); |
|
1731 | 1733 | var input; |
1732 | 1734 | for (var i = 0; i < inputName.length; i++) { |
1733 | 1735 |
|
1734 | | - input = $(node.selector).find('[name="'+ inputName[i] + '"]'); |
| 1736 | + input = $(node.selector).find('[name="' + inputName[i] + '"]'); |
1735 | 1737 | if (!input[0]) { |
1736 | 1738 |
|
1737 | 1739 | // {debug} |
1738 | 1740 | window.Debug.log({ |
1739 | 1741 | 'node': node, |
1740 | 1742 | 'function': '$.removeError()', |
1741 | 1743 | 'arguments': JSON.stringify(inputName[i]), |
1742 | | - 'message': 'ERROR - Unable to find ' + '$(' + node.selector + ').find("[name="'+ inputName[i] + '"]")' |
| 1744 | + 'message': 'ERROR - Unable to find ' + '$(' + node.selector + ').find("[name="' + inputName[i] + '"]")' |
1743 | 1745 | }); |
1744 | 1746 |
|
1745 | 1747 | window.Debug.print(); |
|
1912 | 1914 | console.table(this.table); |
1913 | 1915 | } else { |
1914 | 1916 | $.each(this.table, function (index, data) { |
1915 | | - console.log(data['Name'] + ': ' + data['Execution Time']+'ms'); |
| 1917 | + console.log(data['Name'] + ': ' + data['Execution Time'] + 'ms'); |
1916 | 1918 | }); |
1917 | 1919 | } |
1918 | 1920 |
|
|
1929 | 1931 | }; |
1930 | 1932 | // {/debug} |
1931 | 1933 |
|
1932 | | - String.prototype.capitalize = function() { |
| 1934 | + String.prototype.capitalize = function () { |
1933 | 1935 | return this.charAt(0).toUpperCase() + this.slice(1); |
1934 | 1936 | }; |
1935 | 1937 |
|
1936 | 1938 | if (!Array.prototype.indexOf) { |
1937 | | - Array.prototype.indexOf = function(searchElement, fromIndex) { |
1938 | | - var k; |
1939 | | - if (this == null) { |
1940 | | - throw new TypeError('"this" is null or not defined'); |
1941 | | - } |
1942 | | - var O = Object(this); |
1943 | | - var len = O.length >>> 0; |
1944 | | - if (len === 0) { |
1945 | | - return -1; |
1946 | | - } |
1947 | | - var n = +fromIndex || 0; |
1948 | | - if (Math.abs(n) === Infinity) { |
1949 | | - n = 0; |
1950 | | - } |
1951 | | - if (n >= len) { |
1952 | | - return -1; |
1953 | | - } |
1954 | | - k = Math.max(n >= 0 ? n : len - Math.abs(n), 0); |
1955 | | - while (k < len) { |
1956 | | - if (k in O && O[k] === searchElement) { |
1957 | | - return k; |
1958 | | - } |
1959 | | - k++; |
| 1939 | + Array.prototype.indexOf = function (elt /*, from*/) { |
| 1940 | + var len = this.length >>> 0; |
| 1941 | + |
| 1942 | + var from = Number(arguments[1]) || 0; |
| 1943 | + from = (from < 0) |
| 1944 | + ? Math.ceil(from) |
| 1945 | + : Math.floor(from); |
| 1946 | + if (from < 0) |
| 1947 | + from += len; |
| 1948 | + |
| 1949 | + for (; from < len; from++) { |
| 1950 | + if (from in this && |
| 1951 | + this[from] === elt) |
| 1952 | + return from; |
1960 | 1953 | } |
1961 | 1954 | return -1; |
1962 | 1955 | }; |
|
0 commit comments