D
Dr J R Stockton
Much has been written here on Date Validation; but I don't recall seeing
the following method.
Frequently, a page which calls for date input will also call for one
particular form of date output, which should be provided by a Method or
a Function rather than by in-line code. If that form is acceptable to
new Date, perhaps after simple RegExp modification such as converting
DMY to FFF, then one can validate by, in effect,
D = read S1 ; S2 = write D ; OK = S1 == S2
function DoubleConv(S) {
return new Date(S.replace(/-/g, "/")).ISOlocaldateStr() == S }
// given that ISOlocaldateStr has been added to Date
OK = DoubleConv(S) // Test whether S is Y-mm-dd
Clearly, the method can be modified to return, say, either the Date
Object or NaN; and to use other date formats.
the following method.
Frequently, a page which calls for date input will also call for one
particular form of date output, which should be provided by a Method or
a Function rather than by in-line code. If that form is acceptable to
new Date, perhaps after simple RegExp modification such as converting
DMY to FFF, then one can validate by, in effect,
D = read S1 ; S2 = write D ; OK = S1 == S2
function DoubleConv(S) {
return new Date(S.replace(/-/g, "/")).ISOlocaldateStr() == S }
// given that ISOlocaldateStr has been added to Date
OK = DoubleConv(S) // Test whether S is Y-mm-dd
Clearly, the method can be modified to return, say, either the Date
Object or NaN; and to use other date formats.