1318 |
alexandre_ |
1 |
/*
|
|
|
2 |
Copyright (c) 2004-2006, The Dojo Foundation
|
|
|
3 |
All Rights Reserved.
|
|
|
4 |
|
|
|
5 |
Licensed under the Academic Free License version 2.1 or above OR the
|
|
|
6 |
modified BSD license. For more information on Dojo licensing, see:
|
|
|
7 |
|
|
|
8 |
http://dojotoolkit.org/community/licensing.shtml
|
|
|
9 |
*/
|
|
|
10 |
|
1422 |
alexandre_ |
11 |
|
|
|
12 |
|
1318 |
alexandre_ |
13 |
dojo.provide("dojo.validate.datetime");
|
|
|
14 |
dojo.require("dojo.validate.common");
|
|
|
15 |
dojo.validate.isValidTime = function (value, flags) {
|
|
|
16 |
dojo.deprecated("dojo.validate.datetime", "use dojo.date.parse instead", "0.5");
|
|
|
17 |
var re = new RegExp("^" + dojo.regexp.time(flags) + "$", "i");
|
|
|
18 |
return re.test(value);
|
|
|
19 |
};
|
|
|
20 |
dojo.validate.is12HourTime = function (value) {
|
|
|
21 |
dojo.deprecated("dojo.validate.datetime", "use dojo.date.parse instead", "0.5");
|
|
|
22 |
return dojo.validate.isValidTime(value, {format:["h:mm:ss t", "h:mm t"]});
|
|
|
23 |
};
|
|
|
24 |
dojo.validate.is24HourTime = function (value) {
|
|
|
25 |
dojo.deprecated("dojo.validate.datetime", "use dojo.date.parse instead", "0.5");
|
|
|
26 |
return dojo.validate.isValidTime(value, {format:["HH:mm:ss", "HH:mm"]});
|
|
|
27 |
};
|
|
|
28 |
dojo.validate.isValidDate = function (dateValue, format) {
|
|
|
29 |
dojo.deprecated("dojo.validate.datetime", "use dojo.date.parse instead", "0.5");
|
|
|
30 |
if (typeof format == "object" && typeof format.format == "string") {
|
|
|
31 |
format = format.format;
|
|
|
32 |
}
|
|
|
33 |
if (typeof format != "string") {
|
|
|
34 |
format = "MM/DD/YYYY";
|
|
|
35 |
}
|
|
|
36 |
var reLiteral = format.replace(/([$^.*+?=!:|\/\\\(\)\[\]\{\}])/g, "\\$1");
|
|
|
37 |
reLiteral = reLiteral.replace("YYYY", "([0-9]{4})");
|
|
|
38 |
reLiteral = reLiteral.replace("MM", "(0[1-9]|10|11|12)");
|
|
|
39 |
reLiteral = reLiteral.replace("M", "([1-9]|10|11|12)");
|
|
|
40 |
reLiteral = reLiteral.replace("DDD", "(00[1-9]|0[1-9][0-9]|[12][0-9][0-9]|3[0-5][0-9]|36[0-6])");
|
|
|
41 |
reLiteral = reLiteral.replace("DD", "(0[1-9]|[12][0-9]|30|31)");
|
|
|
42 |
reLiteral = reLiteral.replace("D", "([1-9]|[12][0-9]|30|31)");
|
|
|
43 |
reLiteral = reLiteral.replace("ww", "(0[1-9]|[1-4][0-9]|5[0-3])");
|
|
|
44 |
reLiteral = reLiteral.replace("d", "([1-7])");
|
|
|
45 |
reLiteral = "^" + reLiteral + "$";
|
|
|
46 |
var re = new RegExp(reLiteral);
|
|
|
47 |
if (!re.test(dateValue)) {
|
|
|
48 |
return false;
|
|
|
49 |
}
|
|
|
50 |
var year = 0, month = 1, date = 1, dayofyear = 1, week = 1, day = 1;
|
|
|
51 |
var tokens = format.match(/(YYYY|MM|M|DDD|DD|D|ww|d)/g);
|
|
|
52 |
var values = re.exec(dateValue);
|
|
|
53 |
for (var i = 0; i < tokens.length; i++) {
|
|
|
54 |
switch (tokens[i]) {
|
|
|
55 |
case "YYYY":
|
|
|
56 |
year = Number(values[i + 1]);
|
|
|
57 |
break;
|
|
|
58 |
case "M":
|
|
|
59 |
case "MM":
|
|
|
60 |
month = Number(values[i + 1]);
|
|
|
61 |
break;
|
|
|
62 |
case "D":
|
|
|
63 |
case "DD":
|
|
|
64 |
date = Number(values[i + 1]);
|
|
|
65 |
break;
|
|
|
66 |
case "DDD":
|
|
|
67 |
dayofyear = Number(values[i + 1]);
|
|
|
68 |
break;
|
|
|
69 |
case "ww":
|
|
|
70 |
week = Number(values[i + 1]);
|
|
|
71 |
break;
|
|
|
72 |
case "d":
|
|
|
73 |
day = Number(values[i + 1]);
|
|
|
74 |
break;
|
|
|
75 |
}
|
|
|
76 |
}
|
|
|
77 |
var leapyear = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
|
|
|
78 |
if (date == 31 && (month == 4 || month == 6 || month == 9 || month == 11)) {
|
|
|
79 |
return false;
|
|
|
80 |
}
|
|
|
81 |
if (date >= 30 && month == 2) {
|
|
|
82 |
return false;
|
|
|
83 |
}
|
|
|
84 |
if (date == 29 && month == 2 && !leapyear) {
|
|
|
85 |
return false;
|
|
|
86 |
}
|
|
|
87 |
if (dayofyear == 366 && !leapyear) {
|
|
|
88 |
return false;
|
|
|
89 |
}
|
|
|
90 |
return true;
|
|
|
91 |
};
|
|
|
92 |
|