Skip to content

Commit c4bca32

Browse files
author
t_bertrand
committed
Version 1.5.0
- Added $.addValidationRule to globally add a regex rule (ex: FILENAME see commence inside the code) - Modified NUMERIC regex (to match ex: 1,000.00 or 1000) - Added INTEGER regex - Added flexibility on data-validation-regex attribute
1 parent c572703 commit c4bca32

File tree

3 files changed

+165
-68
lines changed

3 files changed

+165
-68
lines changed

html5-form-validation.jquery.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"validation",
1010
"input"
1111
],
12-
"version": "1.4.0",
12+
"version": "1.5.0",
1313
"author": {
1414
"name": "Tom Bertrand",
1515
"url": "http://www.runningcoder.org/jqueryvalidation/"

jquery.validation.js

Lines changed: 159 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
/**
22
* jQuery Form Validation
3+
* Copyright (C) 2014 RunningCoder.org
4+
* Licensed under the MIT license
35
*
46
* @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/
159
*
1610
* @note
1711
* Remove debug code: //\s?\{debug\}[\s\S]*?\{/debug\}
1812
*/
19-
(function (window, document, $, undefined)
13+
;(function (window, document, $, undefined)
2014
{
2115

2216
window.Validation = {
@@ -45,8 +39,10 @@
4539
var _rules = {
4640
// Validate not empty
4741
NOTEMPTY: /./,
42+
// Validate an integer
43+
INTEGER: /^\d+$/,
4844
// Validate a numeric
49-
NUMERIC: /^[0-9]+$/,
45+
NUMERIC: /^\d+(?:,\d{3})?(?:\.\d+)?$/,
5046
// Validate an alphanumeric string (no special chars)
5147
MIXED: /^[\w\s-]+$/,
5248
// Validate a spaceless string
@@ -71,10 +67,11 @@
7167
* @private
7268
* Error messages
7369
*/
74-
var _messages = Object.preventExtensions({
70+
var _messages = {
7571
'default': '$ contain error(s).',
7672
'NOTEMPTY': '$ must not be empty.',
7773
'NUMERIC': '$ must be numeric.',
74+
'INTEGER': '$ must be an integer.',
7875
'STRING': '$ must be a string.',
7976
'NOSPACE': '$ must not contain spaces.',
8077
'TRIM': '$ must not start or end with space character.',
@@ -90,7 +87,7 @@
9087
'>=': '$ must be greater or equal to % characters.',
9188
'==': '$ must be equal to %',
9289
'!=': '$ must be different than %'
93-
}),
90+
},
9491
_extendedMessages = false;
9592

9693
/**
@@ -578,50 +575,11 @@
578575
// Validates the "data-validation-regex"
579576
if (validationRegex) {
580577

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);
621579

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)) {
623582
return true;
624-
625583
}
626584

627585
try {
@@ -1328,6 +1286,27 @@
13281286

13291287
};
13301288

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+
13311310
// =================================================================================================================
13321311

13331312
/**
@@ -1786,10 +1765,134 @@
17861765

17871766
}
17881767

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+
17891824
}
17901825

17911826
};
17921827

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+
17931896
// {debug}
17941897
window.Debug = {
17951898

0 commit comments

Comments
 (0)