Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
12
rated 0 times [  14] [ 2]  / answers: 1 / hits: 21052  / 12 Years ago, wed, june 27, 2012, 12:00:00

I found a lot of links to validate string if it is a date.



Like here and here.



But anyway I cannot figure out how to validate if we have this thing:



6/6/2012 where first 6 is month and the second 6 is days



and also if user input it like that:



06/06/2012



Any clue how it could be done in a proper way?



Thanks!!


More From » jquery

 Answers
4

Here, this should work with any date format with 4 digit year and any delimiter. I extracted it from my plugin Ideal Forms which validates dates and much more.



var isValidDate = function (value, userFormat) {
var

userFormat = userFormat || 'mm/dd/yyyy', // default format

delimiter = /[^mdy]/.exec(userFormat)[0],
theFormat = userFormat.split(delimiter),
theDate = value.split(delimiter),

isDate = function (date, format) {
var m, d, y
for (var i = 0, len = format.length; i < len; i++) {
if (/m/.test(format[i])) m = date[i]
if (/d/.test(format[i])) d = date[i]
if (/y/.test(format[i])) y = date[i]
}
return (
m > 0 && m < 13 &&
y && y.length === 4 &&
d > 0 && d <= (new Date(y, m, 0)).getDate()
)
}

return isDate(theDate, theFormat)

}

[#84639] Tuesday, June 26, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
travion

Total Points: 137
Total Questions: 96
Total Answers: 103

Location: India
Member since Wed, Aug 4, 2021
3 Years ago
travion questions
Mon, Dec 16, 19, 00:00, 5 Years ago
Sat, Oct 19, 19, 00:00, 5 Years ago
Fri, Sep 20, 19, 00:00, 5 Years ago
Wed, Nov 14, 18, 00:00, 6 Years ago
Sun, Oct 28, 18, 00:00, 6 Years ago
;