Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
76
rated 0 times [  80] [ 4]  / answers: 1 / hits: 183800  / 12 Years ago, mon, march 4, 2013, 12:00:00

I am trying to validate datetime format MM/DD/YYYY. Here is the code I am trying please help.



 function ValidateDate(testdate) {
var Status
var reg = /^(((0[1-9]|[12]d|3[01])/(0[13578]|1[02])/((19|[2-9]d)d{2}))|((0[1-9]|[12]d|30)/(0[13456789]|1[012])/((19|[2-9]d)d{2}))|((0[1-9]|1d|2[0-8])/02/((19|[2-9]d)d{2}))|(29/02/((1[6-9]|[2-9]d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/g;
if (!reg.test(testdate)) {
Status = false;
}
return Status;
}

More From » jquery

 Answers
11

Try your regex with a tool like http://jsregex.com/ (There is many) or better, a unit test.


For a native validation:


function validateDate(testdate) {
var date_regex = /^d{2}/d{2}/d{4}$/ ;
return date_regex.test(testdate);
}

In your case, to validate (MM/DD/YYYY), with a year between 1900 and 2099, I'll write it like that:


function validateDate(testdate) {
var date_regex = /^(0[1-9]|1[0-2])/(0[1-9]|1d|2d|3[01])/(19|20)d{2}$/ ;
return date_regex.test(testdate);
}

[#79861] Saturday, March 2, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
freddiejarretk

Total Points: 612
Total Questions: 103
Total Answers: 88

Location: Armenia
Member since Sat, Dec 31, 2022
1 Year ago
;