|
1 | 1 | /** |
2 | 2 | * jQuery Form Validation |
| 3 | + * Copyright (C) 2014 RunningCoder.org |
| 4 | + * Licensed under the MIT license |
3 | 5 | * |
4 | 6 | * @author Tom Bertrand |
5 | | - * @version 1.4.0 (2014-09-28) |
6 | | - * |
7 | | - * @copyright |
8 | | - * Copyright (C) 2014 RunningCoder. |
9 | | - * |
10 | | - * @link |
11 | | - * http://www.runningcoder.org/jqueryvalidation/ |
12 | | - * |
13 | | - * @license |
14 | | - * Licensed under the MIT license. |
| 7 | + * @version 1.5.0 (2015-02-03) |
| 8 | + * @link http://www.runningcoder.org/jqueryvalidation/ |
15 | 9 | * |
16 | 10 | * @note |
17 | 11 | * Remove debug code: //\s?\{debug\}[\s\S]*?\{/debug\} |
18 | 12 | */ |
19 | | -(function (window, document, $, undefined) |
| 13 | +;(function (window, document, $, undefined) |
20 | 14 | { |
21 | 15 |
|
22 | 16 | window.Validation = { |
|
45 | 39 | var _rules = { |
46 | 40 | // Validate not empty |
47 | 41 | NOTEMPTY: /./, |
| 42 | + // Validate an integer |
| 43 | + INTEGER: /^\d+$/, |
48 | 44 | // Validate a numeric |
49 | | - NUMERIC: /^[0-9]+$/, |
| 45 | + NUMERIC: /^\d+(?:,\d{3})?(?:\.\d+)?$/, |
50 | 46 | // Validate an alphanumeric string (no special chars) |
51 | 47 | MIXED: /^[\w\s-]+$/, |
52 | 48 | // Validate a spaceless string |
|
71 | 67 | * @private |
72 | 68 | * Error messages |
73 | 69 | */ |
74 | | - var _messages = Object.preventExtensions({ |
| 70 | + var _messages = { |
75 | 71 | 'default': '$ contain error(s).', |
76 | 72 | 'NOTEMPTY': '$ must not be empty.', |
77 | 73 | 'NUMERIC': '$ must be numeric.', |
| 74 | + 'INTEGER': '$ must be an integer.', |
78 | 75 | 'STRING': '$ must be a string.', |
79 | 76 | 'NOSPACE': '$ must not contain spaces.', |
80 | 77 | 'TRIM': '$ must not start or end with space character.', |
|
90 | 87 | '>=': '$ must be greater or equal to % characters.', |
91 | 88 | '==': '$ must be equal to %', |
92 | 89 | '!=': '$ must be different than %' |
93 | | - }), |
| 90 | + }, |
94 | 91 | _extendedMessages = false; |
95 | 92 |
|
96 | 93 | /** |
|
578 | 575 | // Validates the "data-validation-regex" |
579 | 576 | if (validationRegex) { |
580 | 577 |
|
581 | | - var pattern = validationRegex.split('/'); |
582 | | - |
583 | | - if (pattern.length > 1) { |
584 | | - |
585 | | - var tmpPattern = ""; |
586 | | - |
587 | | - // Do not loop through the last item knowing its a potential modifier |
588 | | - for (var k = 0; k < pattern.length - 1; k++) { |
589 | | - if (pattern[k] !== "") { |
590 | | - tmpPattern += pattern[k] + '/'; |
591 | | - } |
592 | | - } |
593 | | - // Remove last added "/" |
594 | | - tmpPattern = tmpPattern.slice(0, -1); |
595 | | - |
596 | | - // Test the last item for modifier(s) |
597 | | - if (/[gimsxeU]+/.test(pattern[pattern.length - 1])) { |
598 | | - var patternModifier = pattern[pattern.length - 1]; |
599 | | - } |
600 | | - |
601 | | - pattern = tmpPattern; |
602 | | - } else { |
603 | | - pattern = pattern[0]; |
604 | | - } |
605 | | - |
606 | | - // Validate the regex |
607 | | - try { |
608 | | - |
609 | | - var rule = new RegExp(pattern, patternModifier); |
610 | | - |
611 | | - } catch (error) { |
612 | | - |
613 | | - // {debug} |
614 | | - options.debug && window.Debug.log({ |
615 | | - 'node': node, |
616 | | - 'function': 'validateInput()', |
617 | | - 'arguments': '{pattern: {' + pattern + '}, modifier: {' + patternModifier+ '}', |
618 | | - 'message': 'WARNING - Invalid [data-validation-regex] on input ' + inputName |
619 | | - }); |
620 | | - // {/debug} |
| 578 | + var rule = _buildRegexFromString(validationRegex); |
621 | 579 |
|
622 | | - // Do not block validation if a regexp is bad, only skip it |
| 580 | + // Do not block validation if a regexp is bad, only skip it |
| 581 | + if (!(rule instanceof RegExp)) { |
623 | 582 | return true; |
624 | | - |
625 | 583 | } |
626 | 584 |
|
627 | 585 | try { |
|
1328 | 1286 |
|
1329 | 1287 | }; |
1330 | 1288 |
|
| 1289 | + /** |
| 1290 | + * @public |
| 1291 | + * jQuery public function to add a validation rule. |
| 1292 | + * |
| 1293 | + * @example |
| 1294 | + * $.addValidationRule( |
| 1295 | + * 'FILENAME', |
| 1296 | + * /^[^\\/:\*\?<>\|\"\']*$/, |
| 1297 | + * '$ has an invalid filename.' |
| 1298 | + * ) |
| 1299 | + * |
| 1300 | + * @param {string} name |
| 1301 | + * @param {regex} rule |
| 1302 | + * @param {string} message |
| 1303 | + */ |
| 1304 | + $.fn.addValidationRule = $.addValidationRule = function (name, rule, message) { |
| 1305 | + |
| 1306 | + return _api.addValidationRule(this, name, rule, message); |
| 1307 | + |
| 1308 | + }; |
| 1309 | + |
1331 | 1310 | // ================================================================================================================= |
1332 | 1311 |
|
1333 | 1312 | /** |
|
1786 | 1765 |
|
1787 | 1766 | } |
1788 | 1767 |
|
| 1768 | + }, |
| 1769 | + |
| 1770 | + /** |
| 1771 | + * API method to add a validation rule. |
| 1772 | + * |
| 1773 | + * @example |
| 1774 | + * $.addValidationRule( |
| 1775 | + * 'FILENAME', |
| 1776 | + * /^[^\\/:\*\?<>\|\"\']*$/, |
| 1777 | + * '$ has an invalid filename.' |
| 1778 | + * ) |
| 1779 | + * |
| 1780 | + * @param {object} node |
| 1781 | + * @param {string} name |
| 1782 | + * @param {regex} rule |
| 1783 | + * @param {string} message |
| 1784 | + */ |
| 1785 | + addValidationRule: function (node, name, rule, message) { |
| 1786 | + |
| 1787 | + if (!name || !rule || !message) { |
| 1788 | + // {debug} |
| 1789 | + window.Debug.log({ |
| 1790 | + 'node': node, |
| 1791 | + 'function': '$.addValidationRule(name, rule, message)', |
| 1792 | + 'arguments': JSON.stringify({node: node, rule: rule, message: message}), |
| 1793 | + 'message': 'ERROR - Missing one or multiple parameter(s)' |
| 1794 | + }); |
| 1795 | + |
| 1796 | + window.Debug.print(); |
| 1797 | + // {/debug} |
| 1798 | + return false; |
| 1799 | + } |
| 1800 | + |
| 1801 | + rule = _buildRegexFromString(rule); |
| 1802 | + |
| 1803 | + if (!(rule instanceof RegExp)) { |
| 1804 | + // {debug} |
| 1805 | + window.Debug.log({ |
| 1806 | + 'node': node, |
| 1807 | + 'function': '$.addValidationRule(rule)', |
| 1808 | + 'arguments': rule.toString(), |
| 1809 | + 'message': 'ERROR - Invalid rule' |
| 1810 | + }); |
| 1811 | + |
| 1812 | + window.Debug.print(); |
| 1813 | + // {/debug} |
| 1814 | + return false; |
| 1815 | + } |
| 1816 | + |
| 1817 | + name = name.toUpperCase(); |
| 1818 | + |
| 1819 | + _rules[name] = rule; |
| 1820 | + _messages[name] = message; |
| 1821 | + |
| 1822 | + return true; |
| 1823 | + |
1789 | 1824 | } |
1790 | 1825 |
|
1791 | 1826 | }; |
1792 | 1827 |
|
| 1828 | + /** |
| 1829 | + * @private |
| 1830 | + * Converts string into a regex |
| 1831 | + * |
| 1832 | + * @param {String|Object} regex |
| 1833 | + * @returns {Object|Boolean} rule |
| 1834 | + */ |
| 1835 | + function _buildRegexFromString(regex) { |
| 1836 | + |
| 1837 | + if (!regex || (typeof regex !== "string" && !(regex instanceof RegExp))) { |
| 1838 | + _regexDebug(); |
| 1839 | + return false; |
| 1840 | + } |
| 1841 | + |
| 1842 | + if (typeof regex !== 'string') { |
| 1843 | + regex = regex.toString(); |
| 1844 | + } |
| 1845 | + |
| 1846 | + var separator = regex.charAt(0), |
| 1847 | + index = regex.length - 1, |
| 1848 | + pattern, |
| 1849 | + modifier, |
| 1850 | + rule; |
| 1851 | + |
| 1852 | + while (index > 0) { |
| 1853 | + if (/[gimsxeU]/.test(regex.charAt(index))) { |
| 1854 | + index--; |
| 1855 | + } else { |
| 1856 | + break; |
| 1857 | + } |
| 1858 | + } |
| 1859 | + |
| 1860 | + if (regex.charAt(index) !== separator) { |
| 1861 | + separator = null; |
| 1862 | + } |
| 1863 | + |
| 1864 | + if (separator && index !== regex.length - 1) { |
| 1865 | + modifier = regex.substr(index + 1, regex.length - 1); |
| 1866 | + } |
| 1867 | + |
| 1868 | + if (separator) { |
| 1869 | + pattern = regex.substr(1, index - 1); |
| 1870 | + } else { |
| 1871 | + pattern = regex; |
| 1872 | + } |
| 1873 | + |
| 1874 | + try { |
| 1875 | + rule = new RegExp(pattern, modifier); |
| 1876 | + } catch (error) { |
| 1877 | + _regexDebug(); |
| 1878 | + return false; |
| 1879 | + } |
| 1880 | + |
| 1881 | + return rule; |
| 1882 | + |
| 1883 | + function _regexDebug() { |
| 1884 | + // {debug} |
| 1885 | + window.Debug.log({ |
| 1886 | + 'function': '_buildRegexFromString()', |
| 1887 | + 'arguments': '{pattern: {' + (pattern || '') + '}, modifier: {' + (modifier || '') + '}', |
| 1888 | + 'message': 'WARNING - Invalid regex given: ' + regex |
| 1889 | + }); |
| 1890 | + window.Debug.print(); |
| 1891 | + // {/debug} |
| 1892 | + } |
| 1893 | + |
| 1894 | + } |
| 1895 | + |
1793 | 1896 | // {debug} |
1794 | 1897 | window.Debug = { |
1795 | 1898 |
|
|
0 commit comments