Skip to content

Commit

Permalink
Add ISO 8601 validation, closes #373
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed Jul 31, 2015
1 parent 051e077 commit 7a7afaf
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ $ bower install validator-js
- **isIP(str [, version])** - check if the string is an IP (version 4 or 6).
- **isISBN(str [, version])** - check if the string is an ISBN (version 10 or 13).
- **isISIN(str)** - check if the string is an [ISIN][ISIN] (stock/security identifier).
- **isISO8601(str)** - check if the string is a valid [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) date.
- **isIn(str, values)** - check if the string is in a array of allowed values.
- **isInt(str [, options])** - check if the string is an integer. `options` is an object which can contain the keys `min` and/or `max` to check the integer is within boundaries (e.g. `{ min: 10, max: 99 }`).
- **isJSON(str)** - check if the string is valid JSON (note: uses JSON.parse).
Expand Down
74 changes: 74 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -2095,4 +2095,78 @@ describe('Validators', function () {
]
});
});

it('should validate ISO 8601 dates', function () {
// from http://www.pelagodesign.com/blog/2009/05/20/iso-8601-date-validation-that-doesnt-suck/
test({
validator: 'isISO8601'
, valid: [
'2009-12T12:34'
, '2009'
, '2009-05-19'
, '2009-05-19'
, '20090519'
, '2009123'
, '2009-05'
, '2009-123'
, '2009-222'
, '2009-001'
, '2009-W01-1'
, '2009-W51-1'
, '2009-W511'
, '2009-W33'
, '2009W511'
, '2009-05-19'
, '2009-05-19 00:00'
, '2009-05-19 14'
, '2009-05-19 14:31'
, '2009-05-19 14:39:22'
, '2009-05-19T14:39Z'
, '2009-W21-2'
, '2009-W21-2T01:22'
, '2009-139'
, '2009-05-19 14:39:22-06:00'
, '2009-05-19 14:39:22+0600'
, '2009-05-19 14:39:22-01'
, '20090621T0545Z'
, '2007-04-06T00:00'
, '2007-04-05T24:00'
, '2010-02-18T16:23:48.5'
, '2010-02-18T16:23:48,444'
, '2010-02-18T16:23:48,3-06:00'
, '2010-02-18T16:23.4'
, '2010-02-18T16:23,25'
, '2010-02-18T16:23.33+0600'
, '2010-02-18T16.23334444'
, '2010-02-18T16,2283'
, '2009-05-19 143922.500'
, '2009-05-19 1439,55'
]
, invalid: [
'200905'
, '2009367'
, '2009-'
, '2007-04-05T24:50'
, '2009-000'
, '2009-M511'
, '2009M511'
, '2009-05-19T14a39r'
, '2009-05-19T14:3924'
, '2009-0519'
, '2009-05-1914:39'
, '2009-05-19 14:'
, '2009-05-19r14:39'
, '2009-05-19 14a39a22'
, '200912-01'
, '2009-05-19 14:39:22+06a00'
, '2009-05-19 146922.500'
, '2010-02-18T16.5:23.35:48'
, '2010-02-18T16:23.35:48'
, '2010-02-18T16:23.35:48.45'
, '2009-05-19 14.5.44'
, '2010-02-18T16:23.33.600'
, '2010-02-18T16,25:23:48,444'
]
});
});
});
7 changes: 7 additions & 0 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@
'ru-RU': /^(\+?7|8)?9\d{9}$/
};

// from http://goo.gl/0ejHHW
var iso8601 = /^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24\:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/;

validator.extend = function (name, fn) {
validator[name] = function () {
var args = Array.prototype.slice.call(arguments);
Expand Down Expand Up @@ -626,6 +629,10 @@
return validator.isHexadecimal(str) && str.length === 24;
};

validator.isISO8601 = function (str) {
return iso8601.test(str);
};

validator.ltrim = function (str, chars) {
var pattern = chars ? new RegExp('^[' + chars + ']+', 'g') : /^\s+/g;
return str.replace(pattern, '');
Expand Down
Loading

0 comments on commit 7a7afaf

Please sign in to comment.